简体   繁体   中英

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.

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.

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