Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8. CharacterController charControl;
  9. public float walkSpeed = 4;
  10. public float sprintSpeed;
  11. public bool IsSprinting;
  12.  
  13. void Start()
  14. {
  15. charControl = GetComponent<CharacterController>();
  16. sprintSpeed = walkSpeed * 3;
  17. }
  18.  
  19. void Update()
  20. {
  21. MovePlayer();
  22. }
  23.  
  24. void MovePlayer()
  25. {
  26. float currentSpeed = walkSpeed;
  27.  
  28. float horiz = Input.GetAxis("Horizontal");
  29. float vert = Input.GetAxis("Vertical");
  30.  
  31. Vector3 moveDirSide = transform.right * horiz * currentSpeed;
  32. Vector3 moveDirForward = transform.forward * vert * currentSpeed;
  33.  
  34. charControl.SimpleMove(moveDirSide + moveDirForward);
  35.  
  36. IsSprinting = Input.GetKey(KeyCode.LeftShift);
  37. if (IsSprinting)
  38. {
  39. currentSpeed = sprintSpeed;
  40. }
  41. else
  42. {
  43. currentSpeed = walkSpeed;
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement