简体   繁体   中英

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 .

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

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. 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.

Please help. Thanks in advance.

If you want a physics-based jump don't use MovePosition. MovePosition will just move the rigidbody to a position depending on your interpolation setting. 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! 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.

-If you use MovePosition, use it only in 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.

First you need to call methods of Rigidbody from FixedUpdate because that's where physics is calculated. Secondly multiply your vector value to 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;

but I should use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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