簡體   English   中英

讓相機跟隨玩家對象在 3D 空間中的位置和旋轉(Unity3d)

[英]Have camera follow player -object position and rotation in 3D-space (Unity3d)

我的目標是為我的太空戰斗機概念驗證游戲制作一個平滑的“跟隨相機”。 相機應該在所有軸上匹配目標對象的滾轉。

為此,我從統一答案站點“竊取”並修改了此代碼,它對 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;
}

圖片:

在此處輸入圖片說明

如果你解釋了你想要做什么,我會更確定這個答案,但你應該考慮在currentRotation * Vector3.up而不是transform.up的方向上移動Height 此外,請考慮在調用LookAt時使用currentRotation * Vector3.up設置本地向上方向:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM