c# - 让相机跟随玩家对象在 3D 空间中的位置和旋转(Unity3d)
问题描述
我的目标是为我的太空战斗机概念验证游戏提供一个平滑的“跟随相机”。相机应在所有轴上匹配滚降目标对象。
为此,我从统一的 Answers 站点“窃取”并修改了这段代码,它对 X 和 Y(俯仰和偏航)工作得很好,但它拒绝滚动。
代码:
public float Distance;
public float Height;
public float RotationDamping;
public GameObject Target;
void LateUpdate()
{
var wantedRotationAngleYaw = Target.transform.eulerAngles.y;
var currentRotationAngleYaw = transform.eulerAngles.y;
var wantedRotationAnglePitch = Target.transform.eulerAngles.x;
var currentRotationAnglePitch = transform.eulerAngles.x;
var wantedRotationAngleRoll = Target.transform.eulerAngles.z;
var currentRotationAngleRoll = transform.eulerAngles.z;
currentRotationAngleYaw = Mathf.LerpAngle(currentRotationAngleYaw, wantedRotationAngleYaw, RotationDamping * Time.deltaTime);
currentRotationAnglePitch = Mathf.LerpAngle(currentRotationAnglePitch, wantedRotationAnglePitch, RotationDamping * Time.deltaTime);
currentRotationAngleRoll = Mathf.LerpAngle(currentRotationAngleRoll, wantedRotationAngleRoll, RotationDamping * Time.deltaTime);
var currentRotation = Quaternion.Euler(currentRotationAnglePitch, currentRotationAngleYaw, currentRotationAngleRoll);
transform.position = Target.transform.position;
transform.position -= currentRotation * Vector3.forward * Distance;
transform.LookAt(Target.transform);
transform.position += transform.up * Height;
}
图片:
解决方案
如果您解释了您要做什么,我会更确定这个答案,但是您应该考虑朝Height
方向currentRotation * Vector3.up
而不是transform.up
. 另外,currentRotation * Vector3.up
在调用时考虑使用设置本地向上方向LookAt
:
transform.position = Target.transform.position;
transform.position -= currentRotation * Vector3.forward * Distance;
Vector3 currentUp = currentRotation * Vector3.up;
transform.LookAt(Target.transform, currentUp);
transform.position += currentUp * Height;
推荐阅读
- java - Android 插件 3.0.0+ 数据绑定错误?
- apache-spark - Spark 中的 Drools - 性能
- php - 计算wordpress帖子的数量并回应它
- machine-learning - 如何在python中提取随机森林的决策规则
- javascript - 读取列表中特定元素的数据
- javascript - 在 javascript 文件中为 NodeJS 添加环境变量
- node.js - `$set` 可以替换 mongoDB 中 Boolean 类型的字段值吗?
- youtube-data-api - youtube-v3-api setModerationStatus 没有将其从拒绝更改为已发布
- c# - 从 C# wpf 执行 PowerShell 脚本
- azure-service-fabric - Service Fabric 教程 - 部署到 Azure 群集后无法连接