不考虑性能 很简陋的一个功能,主要是用于角色渲染的观察用,比simplecontroller要好用一点
csharp
using System;
using UnityEngine;
public class CharacterViewer : MonoBehaviour
{
public Transform target; // 人物模型的Transform
public float rotationSpeed = 5f;
public float zoomSpeed = 1f;
public float panSpeed = 0.001f;
private Vector3 lastMousePosition;
void Update()
{
// 旋转
if (Input.GetMouseButton(0))
{
float mouseX = -Input.GetAxis("Mouse X");
// float mouseY = Input.GetAxis("Mouse Y");
target.Rotate(Vector3.up, mouseX * rotationSpeed, Space.World);
// target.Rotate(Vector3.right, -mouseY * rotationSpeed, Space.Self);
}
// 缩放
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0)
{
Vector3 zoomDirection = transform.forward;
transform.position += zoomDirection * (scroll * zoomSpeed);
}
// 平移
if (Input.GetMouseButtonDown(2))
{
lastMousePosition = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
Vector3 delta = Input.mousePosition - lastMousePosition;
Camera.main.transform.Translate(-delta.x * panSpeed, -delta.y * panSpeed, 0);
lastMousePosition = Input.mousePosition;
}
}
}