统一运动 [英] Smooth movement in unity

查看:106
本文介绍了统一运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习团结,我面临着一个无法解决的问题,那就是:但是,无论FPS有多高,我都试图使动作平滑而不像通常的视频游戏.我尝试实现逻辑的方式有几种.

I'm starting to learn unity and I am facing a problem I can't get rid of, which is: however I tried to make the movement smooth it's not like usual video games, no matter how high the FPS is or how many different ways I try to implement the logic.

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

I tried to use fixed update and fixed delta time but nothing seems to make a difference.

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

推荐答案

直接分配速度可以覆盖每帧某些计算.最好使用AddForce,以避免由于重力和压力引起的重大变化.摩擦.

Assigning directly to velocity can overwrite certain calculations each frame. Best to use AddForce to avoid overriding changes due to gravity & friction.

Input.GetAxis放入movement的平滑处理为您做平滑处理.只需将该值乘以速度即可获得新的水平速度.

Let the smoothing that Input.GetAxis puts into movement do the smoothing for you. Just multiply that value by speed to get the new horizontal speed.

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

Also, you're changing velocity, so you don't need to multiply your speed field by 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);
}

这篇关于统一运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆