You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
4.9 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class thirdPersonController : MonoBehaviour {
  4. public string CAMERA_INPUT_X;
  5. public string CAMERA_INPUT_Y;
  6. public string MOVEMENT_INPUT_X;
  7. public string MOVEMENT_INPUT_Y;
  8. public string JUMP_INPUT;
  9. public Animator animator;
  10. public Camera camera;
  11. public GameObject cameraCentre;
  12. public float cameraSpeedX = 250.0f;
  13. public float cameraSpeedY = 120.0f;
  14. public float cameraDistance = 10.0f;
  15. public float maxY = 80;
  16. public float minY = -20;
  17. public bool active = true;
  18. public float movementSpeed;
  19. public bool movementLock;
  20. public float jumpHeight;
  21. public float grip;
  22. public float maxVelocity;
  23. public float maxAirVelocity;
  24. private float curMaxVelocity;
  25. public bool slowed;
  26. private float curSpeed;
  27. public float cameraX;
  28. public float cameraY;
  29. private float movementX;
  30. private float movementY;
  31. private bool grounded;
  32. private Rigidbody rigidbody;
  33. public Collider curCollider;
  34. // Use this for initialization
  35. void Start () {
  36. rigidbody = GetComponent<Rigidbody> ();
  37. cameraX = camera.transform.eulerAngles.x;
  38. cameraY = camera.transform.eulerAngles.y;
  39. }
  40. void FixedUpdate(){
  41. RaycastHit hit;
  42. Ray ray = new Ray(transform.position, -Vector3.up);
  43. if (Physics.Raycast (ray, out hit, 1.3f)) {
  44. grounded = true;
  45. curMaxVelocity = maxVelocity;
  46. if (hit.collider.tag == "moveable")
  47. matchVelocity(hit.collider);
  48. } else {
  49. grounded = false;
  50. curMaxVelocity = maxAirVelocity;
  51. animator.SetBool ("Jump", false);
  52. }
  53. if (slowed)
  54. curSpeed = 700.0f;
  55. else
  56. curSpeed = movementSpeed;
  57. if (active) {
  58. applyMovement ();
  59. Vector3 checkVelocity = rigidbody.velocity;
  60. //Debug.Log ("VelMag: " + checkVelocity.magnitude);
  61. if (checkVelocity.magnitude > maxVelocity){
  62. float vertVelo = checkVelocity.y;
  63. checkVelocity = Vector3.ClampMagnitude (checkVelocity, curMaxVelocity);
  64. checkVelocity.y = vertVelo;
  65. //Debug.Log (checkVelocity);
  66. rigidbody.velocity = checkVelocity;
  67. }
  68. transform.rotation = Quaternion.Euler (new Vector3 (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f));
  69. applyGrip();
  70. applyJump (jumpHeight);
  71. }
  72. animator.SetBool ("Grounded", grounded);
  73. animator.SetFloat("Run speed",new Vector3 (rigidbody.velocity.x, 0.0f,rigidbody.velocity.z ).magnitude * Mathf.Sign(movementY));
  74. Vector3 side = transform.InverseTransformDirection (rigidbody.velocity).normalized;
  75. animator.SetFloat ("Side",side.x);
  76. rigidbody.AddForce (new Vector3 (0.0f, -9.81f,0.0f));
  77. curCollider = null;
  78. }
  79. //}
  80. // Update is called once per frame
  81. void LateUpdate () {
  82. if (active) {
  83. cameraX += Input.GetAxis (CAMERA_INPUT_X) * cameraSpeedX * 0.02f;
  84. cameraY += Input.GetAxis (CAMERA_INPUT_Y) * cameraSpeedY * 0.02f;
  85. }
  86. cameraY = ClampAngle (cameraY, minY, maxY);
  87. Quaternion cameraRotation = Quaternion.Euler (cameraY, cameraX, 0.0f);
  88. Vector3 cameraPosition = cameraRotation * new Vector3(0.0f, 0.0f, -cameraDistance) + cameraCentre.transform.position;
  89. camera.transform.rotation = cameraRotation;
  90. camera.transform.position = cameraPosition;
  91. }
  92. private void applyMovement(){
  93. movementX = Input.GetAxis (MOVEMENT_INPUT_X);
  94. movementY = Input.GetAxis (MOVEMENT_INPUT_Y);
  95. if (movementLock) {
  96. movementX = 0;
  97. if (movementY < 0 && this.name == "Player2")
  98. movementY = 0;
  99. if (movementY > 0 && this.name == "Player1")
  100. movementY = 0;
  101. }
  102. Vector3 velocity = new Vector3 (movementX, 0.0f, movementY) * curSpeed * Time.deltaTime;
  103. //Debug.Log (velocity);
  104. velocity = Quaternion.Euler (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f) * velocity;
  105. rigidbody.AddForce (velocity);
  106. }
  107. private void applyJump(float power){
  108. if (grounded) {
  109. if (Input.GetButtonDown (JUMP_INPUT)) {
  110. rigidbody.AddRelativeForce (0, power, 0);
  111. animator.SetBool ("Jump", true);
  112. }
  113. }
  114. }
  115. private void matchVelocity(Collider target){
  116. Debug.Log ("matching velocity");
  117. //Vector3 velocity = rigidbody.velocity;
  118. //velocity = target.attachedRigidbody.velocity;
  119. //rigidbody.velocity = velocity;
  120. //transform.parent = target.transform;
  121. //rigidbody.isKinematic = true;
  122. }
  123. public void applyGrip(){
  124. float localVelX = transform.InverseTransformDirection (rigidbody.velocity).x;
  125. float localVelZ = transform.InverseTransformDirection (rigidbody.velocity).z;
  126. //Debug.Log ("velocity.y: " + localVelZ);
  127. if (grounded && Mathf.Abs(movementX) < 0.8f ) {
  128. rigidbody.AddForce(transform.right * -localVelX * grip);
  129. if (Mathf.Abs(movementY) < 0.5f && Mathf.Abs(localVelZ )> 1.0f){
  130. rigidbody.AddForce(-transform.forward * grip * localVelZ );
  131. }
  132. }
  133. }
  134. void OnCollisionStay(Collision col) {
  135. if (col.transform.tag == "moveable")
  136. curCollider = col.collider;
  137. }
  138. private static float ClampAngle (float angle, float min, float max) {
  139. if (angle < -360)
  140. angle += 360;
  141. if (angle > 360)
  142. angle -= 360;
  143. return Mathf.Clamp (angle, min, max);
  144. }
  145. }