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.

76 lines
1.9 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 float movementSpeed;
  15. public float jumpHeight;
  16. private float cameraX;
  17. private float cameraY;
  18. private float movementX;
  19. private float movementY;
  20. // Use this for initialization
  21. void Start () {
  22. cameraX = camera.transform.eulerAngles.x;
  23. cameraY = camera.transform.eulerAngles.y;
  24. }
  25. void FixedUpdate(){
  26. movementX = Input.GetAxis (MOVEMENT_INPUT_X);
  27. movementY = Input.GetAxis (MOVEMENT_INPUT_Y);
  28. Vector3 velocity = new Vector3 (movementX, 0.0f, movementY)* movementSpeed * Time.deltaTime;
  29. //Debug.Log (velocity);
  30. velocity = Quaternion.Euler (0.0f,camera.transform.rotation.eulerAngles.y, 0.0f) * velocity;
  31. GetComponent<Rigidbody>().AddForce (velocity);
  32. //if (velocity != Vector3.zero)
  33. transform.rotation = Quaternion.Euler(new Vector3 (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f));
  34. if (Physics.Raycast (transform.position, -Vector3.up, 1.3f)) {
  35. //print ("Raycast succeeded");
  36. if (Input.GetButtonDown (JUMP_INPUT)) {
  37. GetComponent<Rigidbody>().AddRelativeForce (0, jumpHeight, 0);
  38. }
  39. }
  40. }
  41. // Update is called once per frame
  42. void LateUpdate () {
  43. cameraX += Input.GetAxis(CAMERA_INPUT_X) * cameraSpeedX * 0.02f;
  44. cameraY += Input.GetAxis(CAMERA_INPUT_Y) * cameraSpeedY * 0.02f;
  45. Quaternion cameraRotation = Quaternion.Euler (cameraY, cameraX, 0.0f);
  46. Vector3 cameraPosition = cameraRotation * new Vector3(0.0f, 0.0f, -cameraDistance) + cameraCentre.transform.position;
  47. camera.transform.rotation = cameraRotation;
  48. camera.transform.position = cameraPosition;
  49. }
  50. }