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.

216 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. //if (!slowed)
  71. applyJump (jumpHeight);
  72. }
  73. animator.SetBool ("Grounded", grounded);
  74. animator.SetFloat("Run speed",new Vector3 (rigidbody.velocity.x, 0.0f,rigidbody.velocity.z ).magnitude * Mathf.Sign(movementY));
  75. Vector3 side = transform.InverseTransformDirection (rigidbody.velocity).normalized;
  76. animator.SetFloat ("Side",side.x);
  77. rigidbody.AddForce (new Vector3 (0.0f, -9.81f,0.0f));
  78. curCollider = null;
  79. }
  80. //}
  81. // Update is called once per frame
  82. void LateUpdate () {
  83. if (active) {
  84. cameraX += Input.GetAxis (CAMERA_INPUT_X) * cameraSpeedX * 0.02f;
  85. cameraY += Input.GetAxis (CAMERA_INPUT_Y) * cameraSpeedY * 0.02f;
  86. }
  87. cameraY = ClampAngle (cameraY, minY, maxY);
  88. Quaternion cameraRotation = Quaternion.Euler (cameraY, cameraX, 0.0f);
  89. Vector3 cameraPosition = cameraRotation * new Vector3(0.0f, 0.0f, -cameraDistance) + cameraCentre.transform.position;
  90. camera.transform.rotation = cameraRotation;
  91. camera.transform.position = cameraPosition;
  92. }
  93. private void applyMovement(){
  94. movementX = Input.GetAxis (MOVEMENT_INPUT_X);
  95. movementY = Input.GetAxis (MOVEMENT_INPUT_Y);
  96. if (movementLock) {
  97. movementX = 0;
  98. if (movementY < 0 && this.name == "Player2")
  99. movementY = 0;
  100. if (movementY > 0 && this.name == "Player1")
  101. movementY = 0;
  102. }
  103. Vector3 velocity = new Vector3 (movementX, 0.0f, movementY) * curSpeed * Time.deltaTime;
  104. //Debug.Log (velocity);
  105. velocity = Quaternion.Euler (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f) * velocity;
  106. rigidbody.AddForce (velocity);
  107. }
  108. private void applyJump(float power){
  109. if (grounded) {
  110. if (Input.GetButtonDown (JUMP_INPUT)) {
  111. rigidbody.AddRelativeForce (0, power, 0);
  112. animator.SetBool ("Jump", true);
  113. }
  114. }
  115. }
  116. private void matchVelocity(Collider target){
  117. Debug.Log ("matching velocity");
  118. //Vector3 velocity = rigidbody.velocity;
  119. //velocity = target.attachedRigidbody.velocity;
  120. //rigidbody.velocity = velocity;
  121. //transform.parent = target.transform;
  122. //rigidbody.isKinematic = true;
  123. }
  124. public void applyGrip(){
  125. float localVelX = transform.InverseTransformDirection (rigidbody.velocity).x;
  126. float localVelZ = transform.InverseTransformDirection (rigidbody.velocity).z;
  127. //Debug.Log ("velocity.y: " + localVelZ);
  128. if (grounded && Mathf.Abs(movementX) < 0.8f ) {
  129. rigidbody.AddForce(transform.right * -localVelX * grip);
  130. if (Mathf.Abs(movementY) < 0.5f && Mathf.Abs(localVelZ )> 1.0f){
  131. rigidbody.AddForce(-transform.forward * grip * localVelZ );
  132. }
  133. }
  134. }
  135. void OnCollisionStay(Collision col) {
  136. if (col.transform.tag == "moveable")
  137. curCollider = col.collider;
  138. }
  139. private static float ClampAngle (float angle, float min, float max) {
  140. if (angle < -360)
  141. angle += 360;
  142. if (angle > 360)
  143. angle -= 360;
  144. return Mathf.Clamp (angle, min, max);
  145. }
  146. }