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.

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