how can I find the rotation of an object threw a script? or how else can I prevent my car from rolling over?
if I try to get the rotation in the z axis and even print it, it doesn’t work well
Not sure what language you are using, but here is how I’d find an objects rotation using C#:
public float getZRotation () {
return transform.localRotation.z;
}
As long as the script is attatched to the car, that line of code should return whatever the z rotation is.
If you’re trying to ensure that the car does not flip over, you can use the localRotation to test for a certain value.
You can constantly check if the rotation is larger than or less than a specific value. If, when the car is flipped over, a certain rotation is more than 90 degrees, you can just constantly check to see if the value of rotation is greater than that number. If so, you can either transform it in the opposite direction or apply force in the opposite direction.
Maybe you could have a wheel for the car. Then, every frame, you do a late update and place the body of the car right above the wheel.
void LateUpdate()
{
// move car chassis right above wheel
}
It would be similar to what is being done here with the camera:
This Bad Piggies video might be sort of like what I was thinking:
Maybe a bit more advanced than what I had in mind though.
you all helped me. thanks