Unity实现场景漫游相机实现场景漫游相机
本文实例为大家分享了Unity实现场景漫游相机的具体代码,供大家参考,具体内容如下
前言前言
拿到场景后总喜欢在场景里面玩一段时间,那这个脚本就是你的不二选择
代码里加了注释,改起来也很方便。
使用方法使用方法
把脚本拖拽到场景相机上,开箱即用。
WASD前后左右移动
QE为上下
Shift加速
鼠标右键按住旋转视角
ESC退出游戏
源码
#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
#endif
using UnityEngine;
public class SimpleCameraController : MonoBehaviour
{
#region 相机状态
/// <summary>
/// 相机状态
/// </summary>
class CameraState
{
public float yaw;
public float pitch;
public float roll;
public float x;
public float y;
public float z;
public void SetFromTransform(Transform t)
{
pitch = t.eulerAngles.x;
yaw = t.eulerAngles.y;
roll = t.eulerAngles.z;
x = t.position.x;
y = t.position.y;
z = t.position.z;
}
public void Translate(Vector3 translation)
{
Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;
x += rotatedTranslation.x;
y += rotatedTranslation.y;
z += rotatedTranslation.z;
}
public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
{
yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
评论0