gpt4 book ai didi

c# - 增加 Vector3 的 Y 值

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:27 24 4
gpt4 key购买 nike

我现在正在使用 Unity 重新学习 3D 数学并修补示例相机 Controller 。默认情况下,它会聚焦在一个目标上,在本例中是玩家,但我想为其添加一个偏移量,使其聚焦在玩家头顶上方。

namespace UnityStandardAssets._2D
{
public class Camera2DFollow : MonoBehaviour
{
public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;

private float m_OffsetZ;
private Vector3 m_LastTargetPosition;
private Vector3 m_CurrentVelocity;
private Vector3 m_LookAheadPos;

// Use this for initialization
private void Start()
{
m_LastTargetPosition = target.position;
m_OffsetZ = (transform.position - target.position).z;
transform.parent = null;
}


// Update is called once per frame
private void Update()
{
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - m_LastTargetPosition).x;

bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;

if (updateLookAheadTarget)
{
m_LookAheadPos = lookAheadFactor*Vector3.right*Mathf.Sign(xMoveDelta);
}
else
{
m_LookAheadPos = Vector3.MoveTowards(m_LookAheadPos, Vector3.zero, Time.deltaTime*lookAheadReturnSpeed);
}

Vector3 aheadTargetPos = target.position + m_LookAheadPos + Vector3.forward*m_OffsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
transform.position = newPos;

m_LastTargetPosition = target.position;
}
}
}

我想我可以简单地添加以下几行,但是这样做时相机会垂直飞出屏幕。这种方法有什么问题,我怎样才能让这个偏移量真正起作用?

    Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
Vector3 newPos2 = new Vector3(newPos.x, newPos.y + 1, newPos.z);
transform.position = newPos2;

最佳答案

可以通过为玩家添加一个子GameObject并将其设置到所需位置,然后将其用作相机目标来解决此问题。

此外,这样做会使职位的进一步改变更容易进行。

关于c# - 增加 Vector3 的 Y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40067783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com