Unity第三人称摄像机控制实现详解

📌 核心功能概述

  • 平滑跟随角色移动
  • 鼠标控制视角旋转
  • 垂直视角角度限制
  • 特殊场景相机位置切换(如处决动画)

🎮 输入控制系统

1
2
3
_input.x -= GameInputManger.Instance.cameralook.y * _controlSpeed;
_input.y += GameInputManger.Instance.cameralook.x * _controlSpeed;
_input.x = Mathf.Clamp(_input.x, _CameraVerticalMaxAngle.x, _CameraVerticalMaxAngle.y);
参数 说明
_controlSpeed 鼠标灵敏度
_CameraVerticalMaxAngle 视角俯仰限制(避免穿模)

🔄 平滑过渡实现

采用两种平滑技术:

1. 旋转平滑

1
2
3
4
_CameraRotation = Vector3.SmoothDamp(_CameraRotation, 
new Vector3(_input.x, _input.y, 0),
ref _smoothdompSpeed,
_smoothVelocity);

2. 位置平滑

1
2
3
4
5
transform.position = Vector3.Lerp(
this.transform.position,
newpositon,
_smoothTime * Time.deltaTime
);

🎥 特殊场景处理

通过事件系统切换相机模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 事件订阅
GameEventMag.MainInstance.AddEventListener<Transform,float>(
"处决相机位置",
SetFinishTarget
);

// 特殊位置设置
void SetFinishTarget(Transform target, float time) {
IsFinish = true;
_CurrentEnemyTarget = target;
_CamerOffSite = 0.1f; // 近距离特写
TimeManger.MainInstance.TryGetOneTimer(time, ResetTarget);
}

💡 开发技巧

  1. 使用LateUpdate:避免相机抖动
  2. Cursor控制:游戏运行时隐藏鼠标
    1
    2
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
  3. 参数化设计:所有关键参数暴露给Inspector

🛠️ 扩展建议

  1. 添加障碍物检测避免穿墙
  2. 实现镜头碰撞缩放
  3. 添加震动效果增强打击感
    ```