Unity实现相机在场景中的自由移动
输入控制:
- 使用 W、S、A、D、E、Q 控制相机前后左右上下移动;
- 使用 鼠标右键 控制视角旋转。
参数控制:
变量 minDistance 设为小于等于 0 的值时相机能够穿透碰撞体表面(设为大于0的值时,相机到碰撞体前停住时的距离会略小于设定值)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tourcamera : MonoBehaviour
{
public Transform tourCamera;
#region 相机移动参数
public float moveSpeed = 1.0f;
public float rotateSpeed = 90.0f;
public float shiftRate = 2.0f;// 按住Shift加速
public float minDistance = 0.5f;// 相机离不可穿过的表面的最小距离(小于等于0时可穿透任何表面)
#endregion
#region 运动速度和其每个方向的速度分量
private Vector3 direction = Vector3.zero;
private Vector3 speedForward;
private Vector3 speedBack;
private Vector3 speedLeft;
private Vector3 speedRight;
private Vector3 speedUp;
private Vec