简体   繁体   中英

Explain the difference in setting a Rigidbody2D's velocity

So I am pretty new to Unity and I was trying to make a basic pong game. I'm using C# as my weapon of choice and was experimenting with different ways to assign a value to a Rigidbody2D's velocity.

   rb2D.velocity.Set(0f, speed);

and

   rb2D.velocity = new Vector2(0f, -speed);

My conundrum is that the top option doesn't apply the velocity at all, and the bottom one is the only way I can get it to work. Can someone explain to me why the Set method doesn't apply the speed, and if so when would I actually use the Set method?

It's a dead field for c# as far I can remember. Way back when they found out using set could lead to unrealistic behavior and in the documentation recommend you instantiate a new vector2 and apply it to your Rigidbody. Clunky but it's their approach.

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