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.

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