让摄像机跟随播放器-对象在3D空间中的位置和旋转(Unity3d)

弗兰克·R·豪根

我的目的是为我的太空战斗机概念游戏打下一个流畅的“跟随摄像机”。摄像机应匹配从所有方向滚落的目标对象。

为此,我已经从统一的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.uptransform.upcurrentRotation * Vector3.upLookAt

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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Phaser 3 Arcade:将跟随播放器的摄像机的Y设置为固定

(处理中)如何在移动和跟随摄像机时旋转播放器?

unity3d(2d!)-摄像机以播放器为中心,但永远不会超出“地图”范围

尝试在摄像机之后实施播放器时出现Unity Camera组件错误

播放器摄像机瞄准/锁定另一个对象

根据摄像机角度使播放器向前,向右,向左和向后移动

如何将摄像机连接到由HLAPI网络管理器实例化的播放器对象?

摄像机跟随时的对象旋转

摄像机移动时,除播放器外,场景中的所有内容看起来都模糊不清。Unity2D

为什么在Unity3D中摄像机绕z轴旋转?

围绕角色旋转摄像机(上下)Unity3D

将位置从3D摄像机转换为2D摄像机

从3个静态摄像机获得一个角度,该如何恢复其在3d空间中的位置?

控制Unity3D网络播放器的摄像头

Unity3D C#VR:在播放器的某个位置生成对象

Unity3D和OpenCV集成并彼此传递摄像机帧

如何为播放器的摄像头跟随者编写p5.js的代码行?(var Mover;)这是2D游戏

Unity3d。RenderTexture 与视频播放器

让播放器在Unity3D中正确跳转

将实时镜头从摄像机流式传输到Unity3D

Unity3D:具有多球体网格的多摄像机渲染的性能

使摄像机跟随球时,应该控制摄像机位置的是哪一个?球还是相机本身?

Unity3D OnDrag移动3D对象不跟随指针

如何合并摄像机固定位置的两个3D点云。

无法在VLC播放器Ubuntu 12.04上同时打开2个USB摄像机

在VLC播放器中嵌套摄像机视频流

3D成像的准确性和摄像机数量

用鼠标事件计算摄像机的位置和旋转

如何旋转摄像机录制的视频?