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.

122 lines
2.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 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. if (Physics.Raycast (transform.position, -Vector3.up, 1.3f)) {
  32. grounded = true;
  33. } else {
  34. grounded = false;
  35. }
  36. if (active) {
  37. applyMovement();
  38. transform.rotation = Quaternion.Euler (new Vector3 (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f));
  39. applyJump(jumpHeight);
  40. applyGrip();
  41. }
  42. }
  43. // Update is called once per frame
  44. void LateUpdate () {
  45. if (active) {
  46. cameraX += Input.GetAxis (CAMERA_INPUT_X) * cameraSpeedX * 0.02f;
  47. cameraY += Input.GetAxis (CAMERA_INPUT_Y) * cameraSpeedY * 0.02f;
  48. }
  49. Quaternion cameraRotation = Quaternion.Euler (cameraY, cameraX, 0.0f);
  50. Vector3 cameraPosition = cameraRotation * new Vector3(0.0f, 0.0f, -cameraDistance) + cameraCentre.transform.position;
  51. camera.transform.rotation = cameraRotation;
  52. camera.transform.position = cameraPosition;
  53. }
  54. private void applyMovement(){
  55. movementX = Input.GetAxis (MOVEMENT_INPUT_X);
  56. movementY = Input.GetAxis (MOVEMENT_INPUT_Y);
  57. Vector3 velocity = new Vector3 (movementX, 0.0f, movementY) * movementSpeed * Time.deltaTime;
  58. //Debug.Log (velocity);
  59. velocity = Quaternion.Euler (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f) * velocity;
  60. rigidbody.AddForce (velocity);
  61. }
  62. private void applyJump(float power){
  63. if (grounded) {
  64. if (Input.GetButtonDown (JUMP_INPUT)) {
  65. rigidbody.AddRelativeForce (0, power, 0);
  66. }
  67. }
  68. }
  69. private void applyGrip(){
  70. float localVelX = transform.InverseTransformDirection (rigidbody.velocity).x;
  71. float localVelZ = transform.InverseTransformDirection (rigidbody.velocity).z;
  72. //Debug.Log ("velocity.y: " + localVelZ);
  73. if (grounded && Mathf.Abs(movementX) < 0.8f ) {
  74. rigidbody.AddForce(transform.right * -localVelX * grip);
  75. if (movementY < 0.5f && localVelZ > 1.0f){
  76. rigidbody.AddForce(-transform.forward * grip * localVelZ );
  77. }
  78. }
  79. }
  80. }