| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class CameraController : MonoBehaviour
- {
- public GameObject player;
- public Vector3 offset;
- public float distance = 20;
- private Vector2 mouseOrigin;
- private Vector3 cameraOrigin;
- private bool drag = false;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- private void Start()
- {
- }
- private void Update()
- {
- if (Mouse.current.scroll.value != Vector2.zero)
- {
- distance -= Mouse.current.scroll.value.y;
- UpdateCamera();
- }
- }
- // Update is called once per frame
- private void LateUpdate()
- {
- if (Mouse.current.rightButton.isPressed)
- {
- var mousePosition = Mouse.current.position.value;
- if (drag == false)
- {
- drag = true;
- mouseOrigin = mousePosition;
- cameraOrigin = Camera.main.transform.position;
- }
- }
- else
- {
- drag = false;
- }
- if (drag)
- {
- var mousePosition = Mouse.current.position.value;
- var mouseDiff = mouseOrigin - mousePosition;
- var mouseDiff3 = (Vector3)mouseDiff;
- mouseOrigin = mousePosition;
- Camera.main.transform.Translate(mouseDiff3);
- Camera.main.transform.rotation = Quaternion.LookRotation(player.transform.position - Camera.main.transform.position);
- UpdateCamera();
- }
- }
- private void UpdateCamera()
- {
- var direction = Camera.main.transform.position - player.transform.position;
- var normal = Vector3.Normalize(direction);
- Camera.main.transform.position = normal * distance;
- }
- }
|