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.

229 lines
5.4 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. public RaycastHit hit;
  35. // Use this for initialization
  36. void Start () {
  37. rigidbody = GetComponent<Rigidbody> ();
  38. cameraX = camera.transform.eulerAngles.x;
  39. cameraY = camera.transform.eulerAngles.y;
  40. }
  41. void FixedUpdate(){
  42. RaycastHit hit;
  43. RaycastHit hit2;
  44. Vector3 pos1 = transform.position + (-transform.right * 0.7f) + (transform.forward*0.4f); //+ (transform.forward*0.2f);
  45. Vector3 pos2 = transform.position + (-transform.right * 0.7f) +(-transform.forward*0.4f);
  46. Ray ray = new Ray(pos1, -Vector3.up);
  47. Ray ray2 = new Ray (pos2, -Vector3.up);
  48. Debug.DrawRay (pos1, -Vector3.up * 1.3f);
  49. Debug.DrawRay (pos2, -Vector3.up * 1.3f);
  50. if (Physics.Raycast (ray, out hit, 1.3f) || Physics.Raycast (ray2, out hit2, 1.3f)) {
  51. grounded = true;
  52. curMaxVelocity = maxVelocity;
  53. //if (hit.collider.tag == "moveable"||hit2.collider.tag == "moveable")
  54. // matchVelocity(hit.collider);
  55. } else {
  56. grounded = false;
  57. curMaxVelocity = maxAirVelocity;
  58. animator.SetBool ("Jump", false);
  59. }
  60. this.hit = hit;
  61. if (hit2.collider != null)
  62. this.hit = hit2;
  63. if (slowed)
  64. curSpeed = 700.0f;
  65. else
  66. curSpeed = movementSpeed;
  67. if (active) {
  68. applyMovement ();
  69. Vector3 checkVelocity = rigidbody.velocity;
  70. //Debug.Log ("VelMag: " + checkVelocity.magnitude);
  71. if (checkVelocity.magnitude > maxVelocity){
  72. float vertVelo = checkVelocity.y;
  73. checkVelocity = Vector3.ClampMagnitude (checkVelocity, curMaxVelocity);
  74. checkVelocity.y = vertVelo;
  75. //Debug.Log (checkVelocity);
  76. rigidbody.velocity = checkVelocity;
  77. }
  78. transform.rotation = Quaternion.Euler (new Vector3 (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f));
  79. applyGrip();
  80. applyJump (jumpHeight);
  81. }
  82. animator.SetBool ("Grounded", grounded);
  83. animator.SetFloat("Run speed",new Vector3 (rigidbody.velocity.x, 0.0f,rigidbody.velocity.z ).magnitude * Mathf.Sign(movementY));
  84. Vector3 side = transform.InverseTransformDirection (rigidbody.velocity).normalized;
  85. animator.SetFloat ("Side",side.x);
  86. rigidbody.AddForce (new Vector3 (0.0f, -9.81f,0.0f));
  87. curCollider = null;
  88. }
  89. //}
  90. // Update is called once per frame
  91. void LateUpdate () {
  92. if (active) {
  93. cameraX += Input.GetAxis (CAMERA_INPUT_X) * cameraSpeedX * 0.02f;
  94. cameraY += Input.GetAxis (CAMERA_INPUT_Y) * cameraSpeedY * 0.02f;
  95. }
  96. cameraY = ClampAngle (cameraY, minY, maxY);
  97. Quaternion cameraRotation = Quaternion.Euler (cameraY, cameraX, 0.0f);
  98. Vector3 cameraPosition = cameraRotation * new Vector3(0.0f, 0.0f, -cameraDistance) + cameraCentre.transform.position;
  99. camera.transform.rotation = cameraRotation;
  100. camera.transform.position = cameraPosition;
  101. }
  102. private void applyMovement(){
  103. movementX = Input.GetAxis (MOVEMENT_INPUT_X);
  104. movementY = Input.GetAxis (MOVEMENT_INPUT_Y);
  105. if (movementLock) {
  106. movementX = 0;
  107. if (movementY < 0 && this.name == "Player2")
  108. movementY = 0;
  109. if (movementY > 0 && this.name == "Player1")
  110. movementY = 0;
  111. }
  112. Vector3 velocity = new Vector3 (movementX, 0.0f, movementY) * curSpeed * Time.deltaTime;
  113. //Debug.Log (velocity);
  114. velocity = Quaternion.Euler (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f) * velocity;
  115. rigidbody.AddForce (velocity);
  116. }
  117. private void applyJump(float power){
  118. if (grounded) {
  119. if (Input.GetButtonDown (JUMP_INPUT)) {
  120. rigidbody.AddRelativeForce (0, power, 0);
  121. animator.SetBool ("Jump", true);
  122. }
  123. }
  124. }
  125. private void matchVelocity(Collider target){
  126. //Debug.Log ("matching velocity");
  127. //Vector3 velocity = rigidbody.velocity;
  128. //velocity = target.attachedRigidbody.velocity;
  129. //rigidbody.velocity = velocity;
  130. //transform.parent = target.transform;
  131. //rigidbody.isKinematic = true;
  132. }
  133. public void applyGrip(){
  134. float localVelX = transform.InverseTransformDirection (rigidbody.velocity).x;
  135. float localVelZ = transform.InverseTransformDirection (rigidbody.velocity).z;
  136. //Debug.Log ("velocity.y: " + localVelZ);
  137. if (grounded && Mathf.Abs(movementX) < 0.8f ) {
  138. rigidbody.AddForce(transform.right * -localVelX * grip);
  139. if (Mathf.Abs(movementY) < 0.5f && Mathf.Abs(localVelZ )> 1.0f){
  140. rigidbody.AddForce(-transform.forward * grip * localVelZ );
  141. }
  142. }
  143. }
  144. void OnCollisionStay(Collision col) {
  145. if (col.transform.tag == "moveable")
  146. curCollider = col.collider;
  147. }
  148. private static float ClampAngle (float angle, float min, float max) {
  149. if (angle < -360)
  150. angle += 360;
  151. if (angle > 360)
  152. angle -= 360;
  153. return Mathf.Clamp (angle, min, max);
  154. }
  155. }