Velocity of parent local to child
var vel = rigidBody.GetRelativePointVelocity(wheel_fr.localPosition);
var tempvector = wheel_fr.right*-wheel_fr.InverseTransformDirection(vel).x;
rigidBody.AddForceAtPosition(hitnormal_r*spring_strength*hitlength_r+hitnormal_r*hitdelta_r + tempvector*500 ,ray_origin_r,ForceMode.Force);
vel = rigidBody.GetRelativePointVelocity(wheel_fl.localPosition);
tempvector = wheel_fl.right*wheel_fl.InverseTransformDirection(vel).x;
rigidBody.AddForceAtPosition(hitnormal_l*spring_strength*hitlength_l+hitnormal_l*hitdelta_l + tempvector*500 ,ray_origin_l,ForceMode.Force);
So what I'm working on is a car game with ray cast suspension. The car itself is a rigidbody with offset force application for each wheel. I got the suspension done nicely but i can't get the lateral grip to work properly. Essentially what I need is to read the velocity of the main car rigidbody but in the position and axis of the child/wheel. For some reason InverseTransformDirection doesn't do it for me even though alot of my google searches have led me to this function.
I'm lost. Any help appreciated! Thanks in advance
I don't see any major mistakes. However keep in $$anonymous$$d that for this code to work, your "wheel" gameobjects need to be a direct child of the rigidbody, otherwise the localPosition is wrong.
If in doubt you can transform the world position into the local position of the rigidbody by using InverseTransformPoint of the rigidbody Transform.
However in that case it would be easier to use GetPointVelocity and just pass the world space position of the wheels.
Though if the wheels are direct childs of the rigidbody, your method should work just fine.
What exactly does mean:
doesn't do it for me
Your "500" looks a bit strange. magic numbers like this are dangerous. $$anonymous$$eep in $$anonymous$$d that the amount of force to cancel the slip velocity depends on the rigidbody mass. Have you tried using Debug.DrawRay to visualize your tempvector of each wheel?
I also would recommend to put all the logic for one wheel in a seperate method that just takes the wheel transform and a local direction vector specifying the direction of the wheel.
The 500 is just a temporary multiplier to boost the grip level higher so I can see whether it works or not. Right now it doesn't seem to work. If I apply grip to the front wheels only, what I expect to see is that the car just spin out instantly, however I'm getting some very strange results. It appears as if the lateral velocity testing is actually world oriented but I'm not sure. One thing i can safely conclude is that InverseTransformDirection isn't the right function for what I'm doing.
And yes, I have tried drawing lines and it appears to do what I want to but the driving aspect is very glitchy. The car works somewhat driving straight but as soon as you reach 90 degrees turning it starts to glitch out very quickly. Honestly, I'm not sure exactly what it's doing. It changes all the time as I change my experiments. But what I want is to read the velocity of the main rigidbody scoped trough the rotation of the wheel child. Basically.
Answer by p3er · Sep 22, 2016 at 05:41 PM
I think i've managed to further isolate the problem.. I've changed my mind about InverseTransformDIrection and it appears to be working. However it seems as if the offset velocity check doesn't work properly. Specifically this: vel = rigidBody.GetRelativePointVelocity(wheel_rl.localPosition);
I made this conclusion by drawing lines and figuring out that the tyres lateral speed is correct when the car is moving at a considerate speed however not when the car is stationary and rotating.
The wheels are direct childs of the rigidbody gameobject. I also tried subtracting the offset velocity with the rigidbody main velocity and it seems to give a net value of something else than 0 which is highly confusing..
Okay now it all works perfectly! Replaced vel = rigidBody.GetRelativePointVelocity(wheel_rl.localPosition);
with
vel = rigidBody.GetPointVelocity(wheel_rl.position);