首页 > 解决方案 > 统一流畅的运动

问题描述

我开始学习团结,我面临着一个我无法摆脱的问题,那就是:但是我试图让动作变得流畅,它不像通常的视频游戏,无论 FPS 有多高或有多少不同我尝试实现逻辑的方式。

我尝试使用固定更新和固定增量时间,但似乎没有什么不同。

void Update()
{

    movement = Input.GetAxis("Horizontal");
    if ((movement > 0 && lookingLeft) || (movement < 0 && !lookingLeft))
    {
        lookingLeft = !lookingLeft;
        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
    }
    if (noOfJumps > 0 || jumping)
        jumpInput = Input.GetAxis("Jump");
    if (jumpInput > 0 && !jumping)
    {
        pressTime = 0;
        jumping = true;
        noOfJumps--;

    }
    else if (jumpInput == 0)
        jumping = false;
    else
        pressTime += Time.deltaTime;

    if (pressTime > 0.15f)
        jumpInput = 0;

}

private void FixedUpdate()
{
    rd.velocity = Vector2.Lerp(rd.velocity, new Vector2(movement != 0 ? speed *
    movement * Time.fixedDeltaTime : rd.velocity.x * 0.95f, (jumpInput != 0) ? jumpInput * jumpSpeed * Time.fixedDeltaTime : -1), 0.9f);
}

标签: c#unity3d

解决方案


直接分配给速度可以覆盖每帧的某些计算。最好用于AddForce避免由于重力和摩擦导致的压倒性变化。

Input.GetAxis投入movement的平滑为你做平滑。只需将该值乘以速度即可获得新的水平速度。

此外,您正在更改速度,因此您不需要将速度场乘以Time.fixedDeltaTime

private void FixedUpdate()
{
    float newVerticalVelocity = rd.velocity.y + jumpInput * jumpSpeed;
    Vector2 velocityChange =   newVerticalVelocity * Vector2.up 
                             + movement * speed * Vector2.right 
                             - rd.velocity;
    rd.AddForce(velocityChange, ForceMode.VelocityChange);
}

推荐阅读