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.

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