Hi there,
I have a car. It has a rigidbody on the car body then 4 wheel colliders. It moves only along z axis. I want it to jump, I can do so by applying AddForce(vector.up, forceMode.Impulse), however the problem is, that the rigidbody is rarely rotated to a Vector3.zero. So while I want to jump up in world space, it jumps up, in local space.
The result is a jump that essentially flips the car over.
How can I apply a force up in world space?
Thanks
Force is always in world space. From the doc:
If you want relative force you’d use Unity - Scripting API: Rigidbody.AddRelativeForce
Yes, I saw this, however the result is, as I accelerate, the body of the cr, leans back ( a desired effect, showing inertia). If I hit jump (using AddForce(Vector3.up, ForceMode.Impulse), tests showed the car jumping at the angle lean, of the body.
I am using DOTween, transform.DOJump() this seems to work. But I feel i would like to use Unity Phsycis. Anyhow, no worries, thank you.
I’m assuming it’s flipping backwards, yes?
This is a result of your inertia making the car lean back, changing it’s axis. When you apply force to your unstable car, it will, instead of moving straight up, rotate somewhat around it’s back wheel, flipping it backwards. If you want to remedy this, try using addForce only on the rear wheel.
What happens if you directly set it’s velocity on the Y axis (instant movement impulse) ?
1 Like
Maybe:
rigidbody.AddForce(rigidbody.transform.TransformDirection(Vector3.up) * amount);
1 Like
And if that doesn’t work, try
// 100 is arbitrary, set it to whatever you want
rigidbody.velocity = (rigidbody.velocity + new Vector3(0, 100));
It’s a far more simple looking solution but Occam’s razor usually holds.
Ps- if neither of these work, post a picture of your car’s center of mass. You can find it with
public Vector3 getCenter(){
Vector3 center = Vector3.zero;
float tMass = 0f;
// children is all child objects with mass that are significant
foreach(GameObject g in children){
center += g.rigidbody.worldCenterOfMass * g.rigidbody.mass;
tMass += g.rigidbody.mass;
}
return (center / tMass);
}
2 Likes