Car weirdly flips 180 degrees while rotating to ground normal
Hey recently I bumped into a problem that I am not being able to fix. Basically I have a car that is composed my 3 transforms: Car -> Normal -> Mesh. The Normal is child of the car and the mesh is child of the Normal. I want this car to rotate accordingly to the ground normal but also rotate it to steer the car.
The current approach is that I rotate the Car transform on the Y axis to steer and then modify the Normal up vector to correspond to the ground normal. In most cases this is working really well. The problem is when my car is doing a loop(hotwheels kind of thing) and turns upside down. In this moment my car has bug which makes it instantly rotate 180 degrees, in the gif you can see the rotation going from 179 to -180, which is causing weird behaviours on my camera and the car handling itself.
Here is the issue being reproduced in a scene where I rotate the green car and make the Normal transform of the red car match its up vector.
GIF demonstrating the problem (Imgur)
Here is the current code:
Car.eulerAngles = Vector3.Lerp(Car.eulerAngles, new Vector3(0, Car.eulerAngles.y + TurnYAmount, 0), Time.deltaTime * TurnSpeed);
Normal.up = Vector3.Lerp(Normal.up, UpCar.up, Time.deltaTime * NormalChangeSpeed);
Normal.Rotate(new Vector3(0, Car.eulerAngles.y, 0));
I have already tried using Quaternion.LookRotation and Quaternion.FromToRotation, but it didn't solve my problem and created more issues, one of them was that the car stopped to rotating more than 90 degrees.
Here is how the normal is calculated:
if(frontLeftHitFlag && frontRightHitFlag && backLeftFlag && backRightFlag)
{
Vector3 a = backRightHit.point - backLeftHit.point;
Vector3 b = frontRightHit.point - backRightHit.point;
Vector3 c = frontLeftHit.point - frontRightHit.point;
Vector3 d = backRightHit.point - frontLeftHit.point;
// Get the normal at each corner
Vector3 crossBA = Vector3.Cross(b, a);
Vector3 crossCB = Vector3.Cross(c, b);
Vector3 crossDC = Vector3.Cross(d, c);
Vector3 crossAD = Vector3.Cross(a, d);
Debug.DrawRay(backRightHit.point, Vector3.up);
Debug.DrawRay(backLeftHit.point, Vector3.up);
Debug.DrawRay(frontLeftHit.point, Vector3.up);
Debug.DrawRay(frontRightHit.point, Vector3.up);
Vector3 normal = ((crossBA) + crossCB + crossDC + crossAD).normalized;
}
Sorry for the long post, any help is highly appreciated.
This is because cross product of two parallel vectors is zero.
Your answer
Follow this Question
Related Questions
How to use Quaternion.Slerp with transform.LookAt? 3 Answers
Questions about Rotations in unity 1 Answer
Quaternion.RotateTowards for camera positions is buggy 1 Answer
Transform.Rotate with dynamic values 1 Answer
Auto level an object 0 Answers