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.

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