Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace SpriteMan3D
  4. {
  5. /// <summary>
  6. /// A simple Input controller for detecting player actions.
  7. /// </summary>
  8. [RequireComponent(typeof(Rigidbody))]
  9. [RequireComponent(typeof(Collider))]
  10. public class PersonPlayerController : PersonController
  11. {
  12. /// <summary>
  13. /// A character's walking speed.
  14. /// </summary>
  15. public float walkSpeed = 2f;
  16. /// <summary>
  17. /// A character's running speed.
  18. /// </summary>
  19. public float runSpeed = 3f;
  20. /// <summary>
  21. /// A character's jump velocity.
  22. /// </summary>
  23. /// <remarks>
  24. /// Increase your project's Physics gravity and increase this value to make a character jump quickly.
  25. /// </remarks>
  26. public float jumpVelocity;
  27.  
  28. /// <summary>
  29. /// How far to look for the distance to ground.
  30. /// </summary>
  31. public float groundDistanceOffset = 0.2f;
  32.  
  33. /// <summary>
  34. /// The collider used for a mellee attack.
  35. /// </summary>
  36. public Collider attackCollider;
  37.  
  38. /// <summary>
  39. /// Determines if this character can move.
  40. /// </summary>
  41. public bool canMove = true;
  42. /// <summary>
  43. /// Determines if this character can jump.
  44. /// </summary>
  45. public bool canJump = true;
  46. /// <summary>
  47. /// Determines if this character can attack.
  48. /// </summary>
  49. public bool canAttack = true;
  50.  
  51. private Rigidbody rb;
  52. private float distToGround;
  53. private Collider charCollider;
  54.  
  55. public float attackCooldown = 0.2f;
  56. private float attackTimer = 0f;
  57.  
  58. void Start()
  59. {
  60. rb = transform.GetComponent<Rigidbody>();
  61.  
  62. // get the distance to ground
  63. charCollider = GetComponent<Collider>();
  64. distToGround = charCollider.bounds.extents.y;
  65. }
  66.  
  67. Vector3 offset;
  68.  
  69. void Update()
  70. {
  71. IsGrounded = Physics.Raycast(transform.position, -Vector3.up, distToGround + groundDistanceOffset);
  72.  
  73. HandleAttack();
  74. HandleJump();
  75. }
  76.  
  77. void FixedUpdate()
  78. {
  79. HandleMove();
  80. }
  81.  
  82. void HandleMove()
  83. {
  84. if (canMove)
  85. {
  86. var speed = walkSpeed;
  87.  
  88. // detect input movement
  89. var moveHorizontal = Input.GetAxis("Horizontal");
  90. var moveVertical = Input.GetAxis("Vertical");
  91. IsMoving = moveHorizontal != 0 || moveVertical != 0;
  92.  
  93. IsRunning = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  94. if(IsRunning)
  95. {
  96. speed = runSpeed;
  97. }
  98.  
  99. // rotate the character
  100. var movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
  101. var rot = movement * (speed / 10);
  102.  
  103. if (attackTimer <= 0 && movement != Vector3.zero)
  104. {
  105. var newRotation = Quaternion.LookRotation(rot);
  106. rb.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 360f);
  107. }
  108.  
  109. // move the character
  110. if (IsMoving && offset.y != 0f)
  111. {
  112. movement.y = offset.normalized.y * movement.magnitude;
  113. }
  114. movement *= (speed / 10);
  115.  
  116. var characterMovement = transform.position + movement;
  117. if (attackTimer <= 0 || !IsGrounded)
  118. {
  119. rb.MovePosition(characterMovement);
  120. }
  121. }
  122. }
  123.  
  124. private void HandleJump()
  125. {
  126. if (canJump)
  127. {
  128. // detect jump
  129. JumpStarted = Input.GetButtonDown("Jump");
  130.  
  131. // make the character jump
  132. if (JumpStarted && IsGrounded)
  133. {
  134. var velocity = rb.velocity;
  135. velocity.y = jumpVelocity;
  136. rb.velocity = velocity;
  137. }
  138. }
  139. }
  140.  
  141. private void HandleAttack()
  142. {
  143. if (canAttack)
  144. {
  145. if (attackTimer <= 0)
  146. {
  147. // detect attack
  148. AttackStarted = Input.GetButtonDown("Fire1");
  149. if (AttackStarted)
  150. {
  151. attackTimer = attackCooldown;
  152. }
  153. }
  154. else
  155. {
  156. if(AttackStarted)
  157. {
  158. AttackStarted = false;
  159. }
  160. attackTimer -= Time.deltaTime;
  161. }
  162. }
  163. }
  164.  
  165. /// <summary>
  166. /// called as animation event from Attack animation.
  167. /// </summary>
  168. public void StartAttack()
  169. {
  170. attackCollider.enabled = true;
  171. }
  172.  
  173. /// <summary>
  174. /// called as animation event from Attack animation.
  175. /// </summary>
  176. public void EndAttack()
  177. {
  178. attackCollider.enabled = false;
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement