- Home /
car water floating script help.
hi i have part of a script:
for(i=0;i<wheels.length;i++){ if(water){ if(wheels[i].transform.position.y < waterLevel){ rigidbody.drag = .1; var newVector = Vector3(meshWheels[i].transform.position.x,waterLevel,meshWheels[i].transform.position.z); var wheelsDistance = Vector3.Distance(meshWheels[i].transform.position,newVector); var moveVector = Vector3(0, wheelsDistance, 0); if(wheelsDistance >0){
rigidbody.AddForceAtPosition(moveVector,meshWheels[i].transform.position);
}
rigidbody.drag = 2;
rigidbody.mass = 1;
rigidbody.useGravity = false;
}
else{
rigidbody.useGravity = true;
rigidbody.mass = 5000;
}
}
}
and it is supposed to make the car float when under the waterLevel. but it is also supposed to make the car stay up straight..... because it adds force at the position of each wheel with the power of the force being the distance from that wheel when in the water. but it dosn't.
any help would be awesome.
When you say it doesn't straighten up, what exactly does it do? stay at a fixed rotation or sway all over the show?
have you tried adding a multiplier to the force ie. 10, 100? Just to see if something would happen? When you say it does not move much at all, do you mean it doesn't move or it does move a tiny bit?
Answer by N1nja · Jul 11, 2010 at 04:39 AM
if you want it to lay on the water.. so that its roof is facing directly up, just get the Up vector, and ensure its facing Up in world space.. just do some rotations based on the difference from the worlds up matrix, and the vehicles up matrix.. might have some funny transitions, but thats just a quick fix I'd do..
if I understand what you're expecting at least.. I'm to tired to really help out with the maths atm though.. but good luck!
Answer by Novodantis 1 · Jul 25, 2010 at 07:48 AM
I usually find force problems having no noticable effect like this is down to a matter of magnitude. As cncguy suggested, perhaps it's worth putting a multiplier on your force, eg.
var moveVector = Vector3(0, wheelsDistance * multiplier, 0);
where "multiplier' is a var you set earlier, to something like 10 or 100. If it goes mental, no worries, at least it's making a difference. Tweak as necessary :)
You could also try changing the force type to Acceleration or other, as the last argument when calling AddForceAtPosition. Check out the various types of forces here.
Your answer
Follow this Question
Related Questions
How can I make a jet script? 0 Answers
Is there a script for moving a rigidbody without using a character controller? 1 Answer
My car dont move 1 Answer
Rigidbody makes NavmeshAgent stutter in its movement 2 Answers
Rigidbody car physics.... 1 Answer