CameraLogic.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraLogic : MonoBehaviour
  5. {/*
  6. private float X;
  7. private float Y;
  8. // Start is called before the first frame update
  9. void Start()
  10. {
  11. }
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. if (Input.GetKey(KeyCode.W)) this.transform.Translate(0, 0, 0.1f, Space.Self);
  16. if (Input.GetKey(KeyCode.S)) this.transform.Translate(0, 0, -0.1f, Space.Self);
  17. if (Input.GetKey(KeyCode.A)) this.transform.Translate(-0.1f, 0, 0, Space.Self);
  18. if (Input.GetKey(KeyCode.D)) this.transform.Translate(0.1f, 0, 0, Space.Self);
  19. if (Input.GetKey(KeyCode.Space)) this.transform.Translate(0, 0.1f, 0, Space.World);
  20. if (Input.GetKey(KeyCode.LeftControl)) this.transform.Translate(0, -0.1f, 0, Space.World);
  21. if (Input.GetMouseButton(0))
  22. {
  23. X += Input.GetAxis("Mouse Y") * 3f;
  24. Y -= Input.GetAxis("Mouse X") * 3f;
  25. this.transform.localEulerAngles = new Vector3(X, Y, 0);
  26. }
  27. }
  28. */
  29. CharacterController playerController;
  30. Vector3 direction;
  31. public float speed = 10;
  32. public float jumpPower = 5;
  33. public float gravity = 7f;
  34. public float mousespeed = 5f;
  35. public float minmouseY = -45f;
  36. public float maxmouseY = 45f;
  37. float RotationY = 0f;
  38. float RotationX = 0f;
  39. public Transform agretctCamera;
  40. // Use this for initialization
  41. void Start()
  42. {
  43. playerController = this.GetComponent<CharacterController>();
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. float _horizontal = Input.GetAxis("Horizontal");
  49. float _vertical = Input.GetAxis("Vertical");
  50. if (playerController.isGrounded)
  51. {
  52. direction = new Vector3(_horizontal, 0, _vertical);
  53. if (Input.GetKeyDown(KeyCode.Space))
  54. direction.y = jumpPower;
  55. }
  56. direction.y -= gravity * Time.deltaTime;
  57. playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * speed));
  58. RotationX += agretctCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousespeed;
  59. RotationY -= Input.GetAxis("Mouse Y") * mousespeed;
  60. RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);
  61. this.transform.eulerAngles = new Vector3(0, RotationX, 0);
  62. agretctCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0);
  63. }
  64. }