I’m trying to attach a disc brake to the steering wheels of a vehicle but working with rotations in C# is driving me nuts!
Consider the green thing: it should follow the front wheels when steering, but they shouldn’t spin with the wheels.
I tried different approaches:
Transform.lookat: Disc brakes “looking at” the wheels. getting wrong orientation but it works. But any attempt to correct the rotation through scripts screws it up completely…
Vector3s are vectors with dimension 3, ie. 3 variables.
For starters, doing rotations with Euler angles are a pain in the ass. They might be easier to understand intuitively, but they pretty much always lead to problems for beginners/people not mathematically inclined. You really should be using quaternions and the methods Unity provides with them. Quaternions are 4 dimensional (x, y, z, and w), and behave differently to Euler rotations. You don’t need to know how they work though, Unity handles it for you.
Are you using WheelColliders for this? If you are you could just use the steerAngle variable to set the brake rotation with Quaternion.Euler(). I know I said not to use euler angles, but for simple stuff like this and with local rotations it’s fine.
Anyway the basic gist of this is you only need one float to encode the rotation, everything else is constrained because of how you want it to behave. In this case we have the cars up vector, which always points up (relative to the car). The brake only ever rotates around that axis:
Essentially, if you set the local rotation you don’t get headaches from the global transforms. I’m assuming the “brake” is a separate GameObject from the wheel and “wheel” is a WheelCollider, but the idea is the same no matter how you’re storing the wheel steering.