Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. public class PlayerController : MonoBehaviour {
  2.  
  3. public float moveSpeed;
  4. public float moveRate;
  5.  
  6. private Vector3 playerPos;
  7. private float time;
  8.  
  9.  
  10. // Use this for initialization
  11. void Start () {
  12. playerPos = transform.position;
  13. time = 0;
  14. }
  15.  
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. // Does player want to go left or right?
  20. if (Input.GetAxisRaw("Horizontal") != 0 & time > moveRate)
  21. {
  22. MoveHorizontal();
  23. time = 0;
  24. }
  25. // Does player want to go up or down?
  26. if (Input.GetAxisRaw("Vertical") != 0 & time > moveRate)
  27. {
  28. MoveVertical();
  29. time = 0;
  30. }
  31.  
  32. time += Time.deltaTime;
  33.  
  34. }
  35.  
  36. void MoveHorizontal()
  37. {
  38. playerPos = (new Vector3(transform.position.x + moveSpeed * Input.GetAxisRaw("Horizontal"), transform.position.y, 0));
  39. transform.position = playerPos;
  40. }
  41. void MoveVertical()
  42. {
  43. playerPos = (new Vector3(transform.position.x, transform.position.y + moveSpeed * Input.GetAxisRaw("Vertical"), 0));
  44. transform.position = playerPos;
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement