| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 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()
- {
- }
- // 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);
- }
- }
- }
|