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.

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