简体   繁体   English

如何在 Unity 中获得流畅的 2D 跳跃?

[英]How to get smooth 2D jumping in Unity?

I want to make my character jump, but I'm getting very laggy movement.我想让我的角色跳跃,但我的动作非常滞后。 It's like the character is teleporting up instead of slowly rising.这就像角色正在传送而不是慢慢上升。 Have you got any ideas?你有什么想法吗? I'm using tilemap and Unity 2019.1.0f2 .我正在使用 tilemap 和Unity 2019.1.0f2

I've already tried:我已经试过了:

rb.AddForce(Vector2.up * jumpForce) , rb.velocity = Vector2.up * jumpForce , rb.MovePosition(new Vector2(transform.position.x , transform.position.y+jumpForce) . rb.AddForce(Vector2.up * jumpForce) , rb.velocity = Vector2.up * jumpForce , rb.MovePosition(new Vector2(transform.position.x , transform.position.y+jumpForce)

Here's rest of my movement script without the previously mentioned code:这是我的运动脚本的其余部分,没有前面提到的代码:

public float speed;
public float jumpVelocity;
float timeBtwJumps = 0f;
float startTimeBtwJumps = 0.3f;

Rigidbody2D rb;

private void Awake()
{
    rb = GetComponent<Rigidbody2D>();
}                                  

private void Update()
{
    float horizontal = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
    Vector2 position = new Vector2(transform.position.x, transform.position.y);
    rb.MovePosition(new Vector2(position.x + horizontal, position.y));
}

private void FixedUpdate()
{
    if (Input.GetAxisRaw("Vertical") > 0f && timeBtwJumps <= 0f)
    {
        Jump();
        timeBtwJumps = startTimeBtwJumps;
    }
    else
        timeBtwJumps -= Time.deltaTime;
}

void Jump()
{

}

Everything is in class deriving from MonoBehaviour.一切都源于 MonoBehaviour。 And here's my rigidbody and player settings:这是我的刚体和播放器设置:

Body Type: Dynamic 
Material: None
Simulated: true
Use Auto Mass: false
Mass:1
Linear Drag: 0
Angular Drag: 0.05
Gravity Scale:1
Collision Detection: Discrete
Sleeping Mode: Start Awake
Interpolate: None
Freeze Rotation: z-true

The player has Box Collider 2D with default settings.播放器具有默认设置的 Box Collider 2D。

Please help.请帮忙。 Thanks in advance.提前致谢。

If you want a physics-based jump don't use MovePosition.如果您想要基于物理的跳跃,请不要使用 MovePosition。 MovePosition will just move the rigidbody to a position depending on your interpolation setting. MovePosition 只会根据您的插值设置将刚体移动到一个位置。 Since your interpolation is none you get "very laggy movement".由于您的插值没有,您会得到“非常滞后的运动”。

Instead, you should use an impulse force .相反,您应该使用脉冲力 Before you only tried acceleration force.之前你只试过加速力。 Furthermore, your horizontal movement also has to change accordingly to an add force approach.此外,您的水平运动也必须根据增力方法而相应地改变。 Try it like this:像这样尝试:

void Jump()
{ 
    rb.AddForce(Vector2.up*jumpVelocity, ForceMode2D.Impulse);
}

To test this, you should disable your horizontal movement for now.要对此进行测试,您应该暂时禁用水平移动。 Or you could maybe do something like this:或者你可以做这样的事情:

private void Update()
{
    float horizontal = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
    rb.AddForce(horizontal * Vector2.right);
}

A few more things:还有几件事:

-Never check for input inside the FixedUpdate loop! - 永远不要检查 FixedUpdate 循环内的输入! Your game might run with 100 fps and depending on fixed update (its called fix because unity tries to keep the physics update framerate fixed) you maybe miss a button press.您的游戏可能会以 100 fps 的速度运行,并且根据固定更新(它被称为修复,因为 Unity 试图保持物理更新帧速率固定),您可能会错过一次按钮按下。

-If you use MovePosition, use it only in FixedUpdate. - 如果您使用 MovePosition,请仅在 FixedUpdate 中使用它。

-You might want to change how you are checking for jump input, also you might want to check if the player is grounded. - 您可能想要更改检查跳跃输入的方式,也可能想要检查玩家是否接地。

-You should also note, using AddForce for an acceleration force in Update should also be avoided. - 您还应该注意,还应避免在 Update 中使用 AddForce 作为加速力。

First you need to call methods of Rigidbody from FixedUpdate because that's where physics is calculated.首先,您需要从FixedUpdate调用Rigidbody方法,因为这是计算物理的地方。 Secondly multiply your vector value to Time.fixedDeltaTime .其次将您的向量值乘以Time.fixedDeltaTime

Example:例子:

void FixedUpdate()
    {
        rb.MovePosition(rb.position + position * Time.fixedDeltaTime);
    }

I had this problem.我有这个问题。 It was like the character was teleporting up instead of slowly rising.就好像角色正在传送而不是慢慢上升。

I solved it with changing my code.我通过更改代码解决了它。

I used this code: MainBodyRigidBody.velocity = new Vector3 (Delta_X, 0, Delta_Y) * MovementSpeed * Time.deltaTime;我使用了这个代码: MainBodyRigidBody.velocity = new Vector3 (Delta_X, 0, Delta_Y) * MovementSpeed * Time.deltaTime;

but I should use MainBodyRigidBody.velocity +=但我应该使用MainBodyRigidBody.velocity +=

I change My code to :我将我的代码更改为:

MainBodyRigidBody.velocity += new Vector3 (Delta_X, 0, Delta_Y) * MovementSpeed * Time.deltaTime;

I Used above Code for my character Movement but it cause my problem.我将上面的代码用于我的角色运动,但它导致了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM