【问题标题】:Increment Y value of Vector3增加 Vector3 的 Y 值
【发布时间】:2016-10-16 07:04:12
【问题描述】:

我现在正在使用 Unity 重新学习 3D 数学并修改示例相机控制器。默认情况下,它专注于一个目标,在这种情况下是玩家,但是我想为其添加一个偏移量,使其专注于玩家的头部上方。

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;

【问题讨论】:

  • 我会向玩家添加一个子游戏对象并将其设置到所需位置,然后将其用作相机目标。
  • 完成,再次感谢您!
  • 这个问题是关于c#,而不是unityscript

标签: c# android unity3d vector


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您正在使用相机的位置(即transform.position)来计算相机的新位置:

    Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
    

    然后在接下来的两行中增加新位置的 y,然后将其重新分配给相机变换位置。所以每一帧相机的 y 位置都会增加,导致它向上“飞”走。

    您可以在 Start() 方法中设置相机所需的 y 位置,并且在计算期间不要更改它

    void Start() 
    {
        Vector3 cameraPos = transform.position;
        cameraPos.y += 1;
        transform.position = cameraPos;
    }
    

    只要相机的 y 位置在您的 Update() 方法的计算中没有改变,这将起作用,就像您的情况一样

    【讨论】:

    • 我尝试添加它并在之后对其进行一些修改,但无法使其正常工作。我确实更新了原始帖子以包含完整的脚本,但如果有帮助的话。感谢您的回复!
    • 请注意,由于脚本附加到相机,“transform.position”会返回“this”对象(相机)的脚本吗?
    • 是的,如果脚本附加到相机游戏对象,那么 transform.position 就是相机的位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多