unity游戏引擎-在Unity3d中,在无尽的转轮中平滑旋转相机



我正在创建一个类似tample run的无尽奔跑者,我可以成功地在玩家中一起旋转相机,但我希望相机像其他游戏一样平稳移动。请注意,在更新方法中调用了move函数

第三人角色(这也附在玩家身上(

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;
    }

这是卡马拉文字

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);

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

最新更新