My car AI is running fine, but if it collides with another one at a high velocity or if it tries to reset to a good position, if it get stucked or flipped down, it becomes crazy.
The first half of this video, shows how crazy the car becomes.
The second part show that the car runs fine when none of those situations happens.
This code can reproduce this problem:
void unFlip ()
{
if (transform.localEulerAngles.z > 80 transform.localEulerAngles.z < 280)
{
paused = true;//stops movement
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
//disable all collisions to not collide with other cars when
BoxCollider [] colls = GetComponentsInChildren< BoxCollider> ();
foreach (BoxCollider obj in colls)
{
if (obj.collider != null)
{
obj.collider.enabled = false;
}
}
transform.rotation = Quaternion.LookRotation (transform.forward);
transform.localPosition = savedtransform.position + Vector3.up;
foreach (BoxCollider obj in colls)
{
if (obj.collider != null)
{
obj.collider.enabled = true;
}
}
paused = false;
}
}
Does anyone knows how to fix this?
Thanks.
EDIT: Saw the video now. The behaviour of the car (in case the speed values are what you want it to be like - cracy acceleration) looks as if the physics changed, as if the car got really light, which is also why it starts flipping when steering. Does it only happen to the crashed car? Have you checked for the physics variables after the crash?
Yes, it does only happens to crashed cars. If there are no crashes, nothing like this happens.
And no, that crazy acceleration is the problem. The car becomes crazy after a crash.
Do you check if the physics values is the same after crash to the normal values?
Try in script on collision set the physics normal values or velocity or flip values.
Yes, I did check. All values remains the same, but it keeps acelerating like crazy.
Well, I found what was causing that problem. What I was trying to achieve with my code is: When the car crashes it disables all collisions, resets itself to the last good position on track, and for a few seconds, stays without collision, so that it does not appear on top of other car. After a few second the collisions become enabled again.
So, the problem was here:
foreach (BoxCollider obj in colls)
{
if (obj.collider != null)
{
obj.collider.enabled = true;
}
}
Disabling the colliders works fine and gives no error at all. And the Ai could run the track without colliding with another car. But enabling the colliders give that problem. Now I am using the “Physics.IgnoreLayerCollision” instead and it works fine.
So, thank you for your help.
It looks like the car center of gravity is getting messed up, which makes sense since changing colliders reconfigures a RB center. Didnt realise enabling/disabling caused the same thing though.
rigidbody.centerOfGravity = new Vector3(0,-1,0); //(mess around with y value - 0 is probably fine.
after you re-enable the colliders.
I would also recommend ensuring the wheels dont spin up before it hits the ground (or mess around with slip so that the cars dont wheel stand)
Honestly depends on the scale of the Vehicle, or type of Vehicle. I noticed that every time I make a change on the scale in a Vehicle, the mass also requires be minimum (-0.1 etc. …).
Also note that up depending on the Vehicle has many polygons and the boxes colliders.
Not sure if this is the way of unity work or not. all these tested with rigid body 1 (standard).
Even after export the models need to sure what is the current mass value.
need Formula!?
I would avoid messing with the scale of the gameobject, as it scales the physics (for lack of a better description).
The idea is you just set the mass to what you would realistically expect it to be. 1000 would be a tonne, which would be a light car.
Many polygons and box colliders should have zero impact on behaviour.