CameraController.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class CameraController : MonoBehaviour
  4. {
  5. public GameObject player;
  6. public Vector3 offset;
  7. public float distance = 20;
  8. private Vector2 mouseOrigin;
  9. private Vector3 cameraOrigin;
  10. private bool drag = false;
  11. // Start is called once before the first execution of Update after the MonoBehaviour is created
  12. private void Start()
  13. {
  14. }
  15. // Update is called once per frame
  16. private void LateUpdate()
  17. {
  18. if (Mouse.current.rightButton.isPressed)
  19. {
  20. var mousePosition = Mouse.current.position.value;
  21. if (drag == false)
  22. {
  23. drag = true;
  24. mouseOrigin = mousePosition;
  25. cameraOrigin = Camera.main.transform.position;
  26. }
  27. }
  28. else
  29. {
  30. drag = false;
  31. }
  32. if (drag)
  33. {
  34. var mousePosition = Mouse.current.position.value;
  35. var mouseDiff = mouseOrigin - mousePosition;
  36. var mouseDiff3 = (Vector3)mouseDiff;
  37. mouseOrigin = mousePosition;
  38. Camera.main.transform.Translate(mouseDiff3);
  39. Camera.main.transform.rotation = Quaternion.LookRotation(player.transform.position - Camera.main.transform.position);
  40. }
  41. }
  42. }