Advertisement
Guest User

Movement Code

a guest
Apr 18th, 2015
7,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.  
  7. //Movement
  8. public float speed;
  9. public float jump;
  10. float moveVelocity;
  11.  
  12. //Grounded Vars
  13. bool grounded = true;
  14.  
  15. void Update ()
  16. {
  17. //Jumping
  18. if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W))
  19. {
  20. if(grounded)
  21. {
  22. GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
  23. }
  24. }
  25.  
  26. moveVelocity = 0;
  27.  
  28. //Left Right Movement
  29. if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
  30. {
  31. moveVelocity = -speed;
  32. }
  33. if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
  34. {
  35. moveVelocity = speed;
  36. }
  37.  
  38. GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
  39.  
  40. }
  41. //Check if Grounded
  42. void OnTriggerEnter2D()
  43. {
  44. grounded = true;
  45. }
  46. void OnTriggerExit2D()
  47. {
  48. grounded = false;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement