在 Unity3d 中转弯时在无尽的跑步者中平滑地旋转相机

Smoothly rotate camera in endless runner on turns in Unity3d

我正在创建类似无尽的游戏 运行 运行我可以成功地一起在播放器中旋转相机,但我希望相机像其他游戏一样顺畅地移动。 请注意更新方法中调用了移动函数

ThirdpersonCharecter(这个也附在玩家身上)

public void Move(bool left,bool right, bool crouch, bool jump)
{
    if (right) {
        switch (forwardDirection) {
            case 1:
                m_Rigidbody.velocity = new Vector3 (12f, 0, 0f);
                break;

            case 2: 
                m_Rigidbody.velocity = new Vector3 (0f, 0, -12f);
                break;

            case 3: 
                m_Rigidbody.velocity = new Vector3 (0f, 0, 12f);
                break;

            case 4: 
                m_Rigidbody.velocity = new Vector3 (-12f, 0, 0f);
                break;
        }
    } else if (left) {
        switch (forwardDirection) {
            case 1:
                m_Rigidbody.velocity = new Vector3 (-12f, 0, 0f);
                break;

            case 2: 
                m_Rigidbody.velocity = new Vector3 (0f, 0, 12f);
                break;

            case 3: 
                m_Rigidbody.velocity = new Vector3 (0f, 0, -12f);
                break;

            case 4: 
                m_Rigidbody.velocity = new Vector3 (12f, 0, 0f);
                break;
        }
    } else {
        switch (forwardDirection) {
            case 1:
                m_Rigidbody.velocity = new Vector3 (0f, 0f, 10f);
                break;

            case 2: 
                m_Rigidbody.velocity = new Vector3 (10f, 0f, 0f);
                break;

            case 3: 
                m_Rigidbody.velocity = new Vector3 (-10f, 0f, 0f);
                break;

            case 4: 
                m_Rigidbody.velocity = new Vector3 (0f, 0f, -10f);
                break;
        }
    }

    if (crouch && m_Animator.GetCurrentAnimatorStateInfo (0).IsName ("Run")) {
        m_Crouching = true;
    } else {
        m_Crouching = false;
    }

    CheckGroundStatus();

    if (!turn) {
        UpdateAnimator (jump);
    } else {
        if(right)
        {
            turn = false;
            Vector3 temp = transform.rotation.eulerAngles;
            if(temp.y<269f){
                    temp.y=temp.y+90f;   }
                else {
                    temp.y=0;
                }
                transform.eulerAngles = temp;
                switch (forwardDirection) {
                case 1:
                    forwardDirection=2;

                    break;
                case 2: 
                    forwardDirection=4;

                    break;
                case 3: 
                    forwardDirection=1;


                    break;
                case 4: 
                    forwardDirection=3;

                    break;
                }
            }else if(left){
                turn=false;
                Vector3 temp=transform.rotation.eulerAngles;
                if(temp.y>-269f){
                    temp.y=temp.y-90f;   }
                else {
                    temp.y=0;
                }
                transform.eulerAngles = temp;
                switch (forwardDirection) {
                case 1:
                    forwardDirection=3;

                    break;
                case 2: 
                    forwardDirection=1;

                    break;
                case 3: 
                    forwardDirection=4;


                    break;
                case 4: 
                    forwardDirection=2;

                    break;
                }
        }

        }
        right = false;
        left = false;

    }




    void UpdateAnimator(bool jump)
    {

        if (jump) {
            m_Animator.SetBool ("Jump", true);

        } else {
            m_Animator.SetBool ("Jump", false);
        }



    if (m_Crouching) {
        m_Animator.SetBool ("Crouch", true);
        count = 0;
    }
    else {
            count +=1;
        if(count>100)
                m_Animator.SetBool ("Crouch", false);
    }

    }



    public void OnAnimatorMove()
    {

        if (m_IsGrounded && Time.deltaTime > 0)
        {
            Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;

            // we preserve the existing y part of the current velocity.
            v.y = m_Rigidbody.velocity.y;
            m_Rigidbody.velocity = v;
        }
    }

    public void setTurn(bool t){
        turn = t;
    }

这是 camara 脚本

void Start () {
    Vector3 angles = transform.eulerAngles;
    x = angles.x;
    y = angles.y;
}

void LateUpdate () {
    if(!target)
        return;

    y = target.eulerAngles.y;


    // ROTATE CAMERA:
    Quaternion rotation = Quaternion.Euler (x, y, 0);
        transform.rotation = rotation;

    // POSITION CAMERA:

        transform.position = target.position - (rotation * Vector3.forward * distance + new Vector3(0,-targetHeight,0));

}

你试过用lerp移动相机吗?它应该对相机的当前位置和目标位置进行插值,从而实现更平滑的移动。

我在上面链接的文档页面上有一个示例代码,因此实施起来应该不会太困难。您所要做的就是:

transform.position = Vector3.Lerp(currentPositionGoesHere, targetPositionGoesHere, floatNumberHere);

浮动将决定两个位置之间过渡的平滑程度。