target为相机所环绕的物体
相机视野平移是通过移动target目标物体实现相机跟随移动,进而实现平移视野的效果。
using UnityEngine;
public class CameraControl : MonoBehaviour
{
public Transform target;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10;
public float yMinLimit = 5, yMaxLimit = 50;
public float distance = 50, minDistance = 2, maxDistance = 100;
public bool needDamping = true;
float damping = 5f;
public float x = 0f;
public float y = 0f;
private Vector3 m_mouseMovePos;
private Camera camera;
private void Start()
{
camera = GetComponent<Camera>();
}
// Update is called once per frame
void LateUpdate()
{
if (target)
{
//鼠标点击右键,围绕target旋转移动相机,改变视野
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y,