Hi I have a problem and I have no idea how to solve this. So in my scene there is a rectangular wall and inside it there is an object that moved by velocity. When the object is near the wall it should rotate to the other direction to dodge the wall.
Can someone help me to solve this problem or at least give me some clue to solve it ? Thank you.
When you say “moved by velocity,” do you mean using Rigidbody physics?
If so, I would start by scrapping that unless you have some strong reason to use it. To do what you describe with physics is going to be a real pain — you’ll have to apply a force, away from the wall, of just the right strength to make the object go the other way at the right point.
If you just move the object yourself, using the Transform, then this will be pretty easy. You can have your object always move in its local forward direction, and then just check your X position relative to the wall, and when it’s too close, rotate to point the other way.
yeah sir… I want to modify this flocking-test project to what I mentioned above. That project used velocity to move the boids. And I have a problem when trying to figure out on how to change the direction to dodge the wall.
Two approaches come to mind
You can grab the magnitude of velocity of the object (ie speed). Then reassign a new velocity in the new direction with the same magnitude. This will likely lead to some pretty unnatural looking turns. But you can guarantee that the object never crashes.
Or you can use forces. Adding a force normal to current direction of velocity will cause the object to change direction without loosing speed. The magnitude of the force determines how tight the turn is. Note that with this approach the object can still hit the wall if it is going fast enough and the force applied is too low.
Okay I want to try your first approach first.
So far I just now :
- grab magnitude of velocity → grabMagnitude = rb.velocity.magnitude
- reassign new velocity in the new direction → rb.velocity = *I don’t know what value should I assign. Should I just assign it manually like maybe new Vector(3, 2). But that value would not suitable to other boids.
To get the same speed you would want something like
rb.velocity = ((new Vector2(3,2)).normalised) * rb.velocity.magnitude;
Thanks for your answer ! Your code is very helpful ! But honestly I don’t know what’s going on here. I mean how come that equation give the object the same speed ?
My knowledge about math physics are not so good. Or maybe not good at all.
I’d suggest picking up a high school text book and reading up on vectors and Newtonian physics. Very useful if you want to do a lot of physics stuff.
Stepping through
rb.velocity =
Here we are assigning the new velocity
(new Vector2(3,2)).normalised
This produces a vector in the direction of (2,3) with a length of one.
- rb.velocity.magnitude;
Now we multiply by the original velocity.
Altogether we have created a new velocity in a different direction, but with the same magnitude as the original velocity.
Thank you again for your explanations!
When I was in high school I feel like “why on earth I study physics ?” and I don’t really like physics actually. But now I realized that I need to have basic physics in game development. Do you have some reference that makes me easy to understands physics for game development ?
Its pretty much all Netownian physics from highschool. A highschool text book is likely to be just as good as anything else you can find.
Oh okay thanks. Hope I found my highschool text book somewhere else.