Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5.  
  6.  
  7. public class movement : MonoBehaviour
  8. {
  9. public float maxSpeed = 10f;
  10. public float maxJumpSpeed = 100f;
  11. public LayerMask groundLayer;
  12. public float distance = 0.4f;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16.  
  17. }
  18.  
  19.  
  20. bool IsGrounded()
  21. {
  22. Vector2 position = transform.position;
  23. Vector2 direction = Vector2.down;
  24.  
  25.  
  26. RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, groundLayer);
  27. if (hit.collider != null)
  28. {
  29. return true;
  30. }
  31.  
  32. return false;
  33. }
  34.  
  35. // Update is called once per frame
  36. void Update()
  37. {
  38. float move = Input.GetAxis("Horizontal");
  39. GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
  40. if (IsGrounded() && Input.GetAxis("Vertical") > 0)
  41. {
  42. GetComponent<Rigidbody2D>().AddForce( new Vector2(0, maxJumpSpeed ));
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement