Hey everyone. I have posted a few questions before and have always gotten some help so hopefully this time won’t be any different. Anyway, I am making a car racing game and need a way to make it so that the car “sticks” to the track and can’t fall off of it. I don’t know if their is something in the database or what, but if anyone has any ideas I would greatly appreciate it. Thanks.
Do you mean the car has to go upside down, follow a fixed line on the track, stay within the width of the track… ?
Yeah, like when going around a loop so that the car doesnt fall off torwards the top or so that if there is a sudden drop in the track the car wont go flying off into the distance and will instead follow right along the track. Do you get what I’m saying?
If you’re using the physics engine, you can add a force to the car in its downward direction:-
var downforce: float;
rigidbody.AddForce(-transform.up * downforce);
This will tend to push the car onto the road. If the car can fall completely off the side of the road (going over a bridge, say), then you might have to switch the downforce off to stop it behaving strangely as it falls.
Thanks for the help, this seems to be exactly what I need. I did try to implement the code into my car game and there are no errors, but it acts as if nothing is happening. The car behaves the same way as it did before. I’m not surte what I am not doing here, so if you have any further help I would gretaly appreciate it.
Perhaps you could post your car script? It’s not immediately obvious what is going wrong.
Just thinking aloud here, but you might need to look at applying the downward force in relation to the speed the car is moving. If you just blindly apply a force to stick the car to the track, what happens if the player stops the car whilst it’s upside down on the loop?
With a constant force the car will just stay there. If the downward force reduces as the car slows, then the car will fall off the loop which is a bit more realistic.
I got it working…for the most part. I just need to smooth it out a little bit. Thanks for all of your help guys, I really appreciate it.
I have this sytem working, like I said before now, but it doesn’t work exactly how I wanted it to. It pushes the car back down on the track when used, but it does so in the global coordinate space instead of locally just to the car. What I want is for the force to push downward on the y axis of the car no matter which way it is facing. I’m not sure how to go about doing this locally to the car instead of globally. Any help would be greatly appreciated. Oh and I am using the rigidbody.AddForce to apply it.
The car’s local downward direction is -transform.up.
Wow, I was trying to make that whay too difficult. This works just like I wanted. Thank you so much, I appreciate it.