- Home /
Random Turning?
I am making a racing game.
I did the physics weird, but I'm not sure that's the problem.
The physics work like this : The Car is a rectangular box collider. The velocity is changed to 50 every update. Steering is done by adding a torque on the axis. No wheels are involved. Basically it is just a block sliding and turning. It drives on friction free items only. The velocity is instantly changed to forward by:
rigidbody.velocity = rigidbody.velocity.magnitude * transform.forward;
every frame. This works perfectly on terrain but when it is driving on cubes instead of a terrain, it sometimes, rarely, but it happens, randomly turns really hard like 90 degrees or it will sometimes randomly jump in the air.
I have tried changing it to adding a force instead of changing the velocity and that really makes no difference. It still turns randomly.
Any ideas on how to fix this?
Or at least what causes it?
I figured something else out: it happens when switching colliders that it is sliding on. For example, it slides over cube one and then jumps randomly when it transfers to cube two.
If you're talking about cubes as basically a bunch of cube/square colliders aligned together, that's a core issue with Unity that's existed for a while now, and is a huge issue with the 2D toolset as well. It will sometimes detect the edge of the cube, even though it's flush with the ground, and collide with it, resulting in you either spinning away, being launched upward, or co$$anonymous$$g to a dead stop.
It is in FixedUpdate. Is there any way I can get around this glitch? And yes I'm talking about cubes aligned together.
The way the Unity $$anonymous$$m suggests is to use a Sphere collider ins$$anonymous$$d... As if this is fine for any situation, but it's not for many people =/ It might be for your game though? You could still have your box collider for colliding with everything else, but have it raised slightly from your sphere collider (perhaps put a sphere collider in each wheel well even as pretend wheels)
Answer by TheCount · Jan 18, 2014 at 10:19 PM
I solved the problem, for anyone else experiencing this problem.
Turn off gravity
set a raycast to the ground of a small distance
If the raycast doesn't hit, then make a downward force to simulate gravity
if you are over the ground, transform the y(vertical) position of your object to a small amount higher than the object it hits.
This way, you are floating just above the ground. It works for me.
Your answer
Follow this Question
Related Questions
Help with ramdom and rotation on a car 0 Answers
inertiaTensor stopping automatic rotation 1 Answer
How to check if an object is upside-down? 2 Answers
Car tutorial physics models rotating randomly? 1 Answer
car suspension and torque help 0 Answers