Advertisement
Guest User

YOU MUST NAME THIS FILE Player_Control

a guest
Feb 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class Player_Control : MonoBehaviour {
  7.  
  8.  
  9.  
  10. public bool isGrounded;
  11. public bool isCrouching;
  12.  
  13. private float speed;
  14. private float w_speed = 0.05f;
  15. private float r_speed = 0.1f;
  16. private float c_speed = 0.025f;
  17. public float rotSpeed;
  18. public float jumpHeight;
  19. Rigidbody rb;
  20. Animator anim;
  21. CapsuleCollider col_size;
  22.  
  23. // Use this for initialization
  24. void Start () {
  25. rb = GetComponent<Rigidbody>();
  26. anim = GetComponent<Animator>();
  27. col_size = GetComponent<CapsuleCollider>();
  28. isGrounded = true;
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update () {
  33.  
  34. //Toggle Crouch
  35. if (Input.GetKeyDown(KeyCode.LeftControl))
  36. {
  37. if (isCrouching)
  38. {
  39. isCrouching = false;
  40. anim.SetBool("isCrouching", false);
  41. col_size.height = 2;
  42. col_size.center = new Vector3(0, 1, 0);
  43. }
  44. else
  45. {
  46. isCrouching = true;
  47. anim.SetBool("isCrouching", true);
  48. speed = c_speed;
  49. col_size.height = 1;
  50. col_size.center = new Vector3(0, 0.5f, 0);
  51. }
  52. }
  53.  
  54. var z = Input.GetAxis("Vertical") * speed;
  55. var y = Input.GetAxis("Horizontal") * rotSpeed;
  56. transform.Translate(0, 0, z);
  57. transform.Rotate(0, y, 0);
  58.  
  59. if(Input.GetKey(KeyCode.Space) && isGrounded == true)
  60. {
  61. rb.AddForce(0, jumpHeight, 0);
  62. anim.SetTrigger("isJumping");
  63. isCrouching = false;
  64. isGrounded = false;
  65. }
  66.  
  67. if (isCrouching)
  68. {
  69. //Crouching Controls
  70. if (Input.GetKey(KeyCode.W))
  71. {
  72. anim.SetBool("isWalking", true);
  73. anim.SetBool("isRunning", false);
  74. anim.SetBool("isIdle", false);
  75. }
  76. else if (Input.GetKey(KeyCode.S))
  77. {
  78. anim.SetBool("isWalking", true);
  79. anim.SetBool("isRunning", false);
  80. anim.SetBool("isIdle", false);
  81. }
  82. else
  83. {
  84. anim.SetBool("isWalking", false);
  85. anim.SetBool("isRunning", false);
  86. anim.SetBool("isIdle", true);
  87. }
  88. }
  89. else if (Input.GetKey(KeyCode.LeftShift))
  90. {
  91. speed = r_speed;
  92. //Running Controls
  93. if (Input.GetKey(KeyCode.W))
  94. {
  95. anim.SetBool("isWalking", false);
  96. anim.SetBool("isIdle", false);
  97. anim.SetBool("isRunning", true);
  98. }
  99. else if (Input.GetKey(KeyCode.S))
  100. {
  101. anim.SetBool("isWalking", false);
  102. anim.SetBool("isIdle", false);
  103. anim.SetBool("isRunning", true);
  104. }
  105. else
  106. {
  107. anim.SetBool("isWalking", false);
  108. anim.SetBool("isRunning", false);
  109. anim.SetBool("isIdle", true);
  110. }
  111. }
  112. else if (!isCrouching)
  113. {
  114. speed = w_speed;
  115. //Standing Controls
  116. if (Input.GetKey(KeyCode.W))
  117. {
  118. anim.SetBool("isWalking", true);
  119. anim.SetBool("isRunning", false);
  120. anim.SetBool("isIdle", false);
  121. }else if (Input.GetKey(KeyCode.S))
  122. {
  123. anim.SetBool("isWalking", true);
  124. anim.SetBool("isRunning", false);
  125. anim.SetBool("isIdle", false);
  126. }else
  127. {
  128. anim.SetBool("isWalking", false);
  129. anim.SetBool("isRunning", false);
  130. anim.SetBool("isIdle", true);
  131. }
  132.  
  133. }
  134.  
  135. }
  136.  
  137.  
  138. void OnCollisionEnter()
  139. {
  140. isGrounded = true;
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement