How do I limit the roll of a game object?
Hey there, I'm trying to develop a simple driving game. Right now the car drives forwards, backward and can turn left or right. When the car takes a sharp turn it has a possibility to roll over. I want the car to tip, but not to topple over. I want at least two wheels on the ground at all times. The car should never tip over, but be able to tip to about 45 degrees left or right (depending on which way you're turning) I've tried to use the Mathf.Clamp method, but then a new issue happens. The car refuses to roll over at all until it hits 45 degrees. Then it will pop over to two wheels and remain there until the game is stopped. Below is the code I've written and causes the car to 'pop' onto two wheels.
var tiltThreshold = transform.localEulerAngles;
if (tiltThreshold.z > 44)
{
tiltThreshold.z = Mathf.Clamp(tiltThreshold.z, -45, 45);
transform.localEulerAngles = tiltThreshold;
}
else if (tiltThreshold.z < 44)
{
tiltThreshold.z = Mathf.Clamp(tiltThreshold.z, -45, 45);
transform.localEulerAngles = tiltThreshold;
}
To reiterate I want to limit the car's roll to 45 degrees to the left or right then when the car is finished turning or turns in a different direction, it drops back down to all four wheels. any advice, direction or suggestions would be appreciated. Thank you!
Your answer
Follow this Question
Related Questions
How to find a position's angle around an arbitrary axis? 1 Answer
Help with clamping rotation, and using animations 0 Answers
How can I fix rotation of Z axis to 0? 0 Answers
Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers
How to Rotate Y axis of an Object toward virtual joystick direction 0 Answers