Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player_Movement : MonoBehaviour {
  5.  
  6. //Player Status
  7. public bool PlayerControl;
  8.  
  9.  
  10. //Dependencies
  11. private CharacterController controller;
  12. //PlayerMovement
  13. public float speed;
  14. public float walk_Speed;
  15. public float run_Speed;
  16. public float sprint_Speed;
  17. public float jumpSpeed;
  18. public float gravity = 5.0f;
  19. public float distanceToGround;
  20. public bool Grounded;
  21. private Vector3 moveDirection = Vector3.zero;
  22.  
  23. //Sprinting
  24. public float SprintSmooth = .5f;
  25.  
  26.  
  27.  
  28.  
  29. void Start()
  30. {
  31. controller = GetComponent<CharacterController>();
  32. }
  33.  
  34. void Update()
  35. {
  36. //If we can control the player then move
  37. if(PlayerControl)
  38. {
  39. Movement();
  40. DetectIfIsGrounded();
  41. }
  42.  
  43. }
  44. void Movement()
  45. {
  46.  
  47.  
  48.  
  49. if(Grounded)
  50. {
  51. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  52. moveDirection = transform.TransformDirection(moveDirection);
  53. moveDirection *= speed;
  54. if(Input.GetAxis("Horizontal") > .02f || Input.GetAxis("Horizontal") < -.02f || Input.GetAxis("Vertical") > .02f || Input.GetAxis("Vertical") < -.02f)
  55. {
  56. if(Input.GetButton("Sprint"))
  57. {
  58. speed = Mathf.Lerp(speed,sprint_Speed, SprintSmooth*10* Time.deltaTime);
  59. }
  60. else
  61. {
  62. speed = Mathf.Lerp(speed,run_Speed, SprintSmooth*10 * Time.deltaTime);
  63. }
  64. }
  65. else
  66. {
  67. speed = 0f;
  68. }
  69. if (Input.GetButton("Jump"))
  70. {
  71. moveDirection.y = jumpSpeed;
  72. }
  73.  
  74.  
  75.  
  76.  
  77. }
  78. else
  79. {
  80. moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
  81. moveDirection = transform.TransformDirection(moveDirection);
  82. moveDirection.x *= speed;
  83. moveDirection.z *= speed;
  84. }
  85.  
  86.  
  87. moveDirection.y -= gravity * Time.deltaTime;
  88.  
  89. controller.Move(moveDirection * Time.deltaTime);
  90.  
  91.  
  92.  
  93. }
  94. void DetectIfIsGrounded () {
  95.  
  96. if(distanceToGround <= 1.5f && controller.isGrounded)
  97. {
  98.  
  99. Grounded = true;
  100. }
  101. else
  102. {
  103. Grounded = false;
  104. }
  105.  
  106. RaycastHit hit = new RaycastHit();
  107. // LayerMask layerMask = ~(1 << LayerMask.NameToLayer("Player"));
  108. if (Physics.Raycast(transform.position, -Vector3.up, out hit))
  109. {
  110. distanceToGround = hit.distance;
  111. }
  112. // Grounded = Physics.Raycast(transform.position,-transform.up, out hit, 0.3f, layerMask);
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement