简体   繁体   中英

Why isn't my bullet moving by using rb.velocity?

I'm trying to make a little space shooter game all by myself and I ran into a problem when trying to make my bullet prefab. I'm setting its velocity in the start method and when I press play it isn't moving at all.

public float speed;
Rigidbody2D rb;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    rb.velocity = transform.forward * speed;
}

I'm using rb.velocity in my player script too, to move him and it works just fine.

It's not working because you put rb.velocity = transform.forward * speed; into the Start function.

Try this script instead

public float speed;
Rigidbody2D rb;

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

}

void Update()
{
    rb.velocity = transform.forward * speed;
}

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