Dani Az.
Dani Az.

Reputation: 3

Why is this script slowing down the gravity of the rigidbody2d (Unity)

This is the script, it's a simple horizontal movement script.

private Rigidbody2D rb;

public float speed;

private float moveHori;

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

void Update()
{
    moveHori = Input.GetAxisRaw("Horizontal");
}

void FixedUpdate()
{
    rb.velocity = new Vector2(moveHori * speed, 0) * Time.deltaTime;
} 

I don't know why is the gravity slowing down.

Upvotes: 0

Views: 305

Answers (1)

derHugo
derHugo

Reputation: 90580

because you set the Y component of the velocity to 0 in

rb.velocity = new Vector2(moveHori * speed, 0) * Time.deltaTime;

rather keep your current Y velocity like

rb.velocity = new Vector2(moveHori * speed, rb.velocity.y);

Note btw that a velocity is framerate independent and you do not want to multiply by Time.deltaTime here! Rather adjust your speed so it is the desired Unity units per seconds.

Upvotes: 3

Related Questions