using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCtrl02 : MonoBehaviour
{
private Vector3 newPos;
private Vector3 newZoom;
private Quaternion newRota;
[SerializeField] private float panSpeed = 1;
[SerializeField] private float moveTime = 1;
[SerializeField] private float rotationAmount = 1;
[SerializeField] private Vector3 zoomAmount = new Vector3(0,-1,1);
private Vector3 dragStartPos, dragCurrentPos;//鼠标拖拽起始点,鼠标拖拽当前位置
private Vector3 rotateStart, rotateCurrent;
private Transform cameraTrans;
private void Start()
{
newPos = transform.position;
newRota = transform.rotation;
cameraTrans = transform.GetChild(0);
newZoom = cameraTrans.localPosition;
}
void Update()
{
HandleMovementInput();//通过键盘控制相机
HandleMouseInput();//通过鼠标控制相机
}
private void HandleMouseInput()
{
if (Input.GetMouseButtonDown(0))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if (plane.Raycast(ray,out distance))
{
dragStartPos = ray.GetPoint(distance);
}
}
if (Input.GetMouseButton(0))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if (plane.Raycast(ray, out distance))
{
dragCurrentPos = ray.GetPoint(distance);
Vector3 difference = dragStartPos - dragCurrentPos;
newPos = transform.position + difference;
}
}
newZoom += Input.mouseScrollDelta.y * zoomAmount;
if (Input.GetMouseButtonDown(2))
rotateStart = Input.mousePosition;
if (Input.GetMouseButton(2))
{
rotateCurrent = Input.mousePosition;
Vector3 difference = rotateStart - rotateCurrent;
rotateStart = rotateCurrent;
newRota *= Quaternion.Euler(Vector3.up * -difference.x / 20);
}
}
private void HandleMovementInput()
{
//移动
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
newPos += transform.forward * panSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
newPos -= transform.forward * panSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
newPos += transform.right * panSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
newPos -= transform.right * panSpeed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, newPos, moveTime * Time.deltaTime);
//旋转
if (Input.GetKey(KeyCode.Q))
newRota *= Quaternion.Euler(Vector3.up * rotationAmount);
if (Input.GetKey(KeyCode.E))
newRota *= Quaternion.Euler(Vector3.down * rotationAmount);
transform.rotation = Quaternion.Lerp(transform.rotation, newRota, moveTime * Time.deltaTime);
//缩放
if (Input.GetKey(KeyCode.R))
newZoom += zoomAmount;
if (Input.GetKey(KeyCode.F))
newZoom -= zoomAmount;
cameraTrans.localPosition = Vector3.Lerp(cameraTrans.localPosition, newZoom, moveTime * Time.deltaTime);
}
}
参考B站up主 :BeaverJoe
————————————————
版权声明:本文为CSDN博主「Fanstasic」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Fanstasic/article/details/124865474
unity 相机鼠标控制移动旋转缩放
猜你喜欢
转载自blog.csdn.net/qq_21743659/article/details/129884350
今日推荐
周排行