EDIT:
IT WORKS! ;)
Thanks again!
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalI found the solution myself after a (bad) night's sleep on it.
void Update () { // NOTE: I am really bad at this (vectors, cross products, dot products etc), hence my comments will be to the best of my understanding. // Time that debug lines and rays will be displayed for. var duration = Time.deltaTime; // Draws a line through the south/north poles of the sphwere. Debug.DrawLine(Vector3.down * 100, Vector3.up * 100, Color.gray, duration, false); // Gravity that affects the vehicle var gravity = Vector3.Normalize(planet.transform.position - transform.position) * 9.81F; // Cross products of gravity to describe planes. var gravityEast = Vector3.Cross(gravity.normalized, Vector3.down).normalized; var gravityNorth = Vector3.Cross(gravityEast.normalized, -gravity.normalized).normalized; // Draw vehicle forward, right and up vectors. Makes it easier to see how it's currently rotated. Debug.DrawRay(transform.position, transform.forward * 0.5F, Color.cyan, duration, false); Debug.DrawRay(transform.position, transform.right * 0.5F, Color.cyan, duration, false); Debug.DrawRay(transform.position, transform.up * 0.5F, Color.cyan, duration, false); // Draw gravity vector and it's plane vectors. Debug.DrawRay(transform.position, gravity.normalized, new Color(0.0F, 0.0F, 0.5F), duration, false); Debug.DrawRay(transform.position, gravityEast, new Color(0.5F, 0.5F, 0.0F), duration, false); Debug.DrawRay(transform.position, gravityNorth, new Color(0.5F, 0.0F, 0.0F), duration, false); // Debug text to verify the absolute values of the gravity and it's plane vectors. text.text = "gravity: " + gravity + "\n"; text.text += "gravityEast: " + gravityEast + "\n"; text.text += "gravityNorth: " + gravityNorth + "\n\n"; // Define "aligned" (plane) vectors that rotate with the vehicle. // For example, gravityAlignedRight will stay pointing along the height plane over the // surface of the sphere and rotate with the vehicle always pointing to the right. // This is used to determine angular difference between vehicles right vector and gravity's aligned right vector. // The reason we need that is because Vector3.Angle() would measure the whole angle between a fixed vector such as // gravityEast and the vehicle right vector otherwise. // gravityAlignedForward is used for yaw and pitch. // gravityAlignedDown is used for pitch and is needed to get the correct dot product for pitch. var gravityAlignedRight = Vector3.Cross(gravity.normalized, -transform.forward).normalized; var gravityAlignedForward = Vector3.Cross(gravity.normalized, gravityAlignedRight).normalized; var gravityAlignedDown = Vector3.Cross(gravityAlignedRight, transform.forward).normalized; // Draw aligned gravity vectors for ease of debugging. Debug.DrawRay(transform.position, gravityAlignedForward * 0.25F, Color.green, duration, false); Debug.DrawRay(transform.position, gravityAlignedRight * 0.25F, Color.magenta, duration, false); Debug.DrawRay(transform.position, gravityAlignedDown * 0.25F, Color.red, duration, false); // And draw the absolute values which helped me set them up. // That is, i had to experiment my way forward to each of these... text.text += "gravityAlignedRight: " + gravityAlignedRight + "\n"; text.text += "gravityAlignedForward: " + gravityAlignedForward + "\n"; text.text += "gravityAlignedDown: " + gravityAlignedDown + "\n\n"; // Get dot products for yaw, pitch and roll so we can set the appropriate sign on the final YPR readings. var yawDot = Vector3.Dot(gravityAlignedForward, gravityEast); var pitchDot = -Vector3.Dot(gravityAlignedDown, gravityAlignedForward); var rollDot = -Vector3.Dot(transform.up, gravityAlignedRight); // Measure angle between the aligned gravity vectors and the vehicles forward and right vectors. // yaw is special in that it uses the aligned forward gravity vector. Don't ask me why, i wouldn't know... ;) var yaw = Vector3.Angle(gravityAlignedForward, gravityNorth) * Mathf.Sign(yawDot); var pitch = Vector3.Angle(transform.forward, gravityAlignedForward) * Mathf.Sign(pitchDot); var roll = Vector3.Angle(transform.right, gravityAlignedRight) * Mathf.Sign(rollDot); // Debug text showing the results of all this... It works, finally. text.text += "yawDot: " + yawDot + "\n"; text.text += "pitchDot: " + pitchDot + "\n"; text.text += "rollDot: " + rollDot + "\n\n"; text.text += "yaw: " + yaw + "\n"; text.text += "pitch: " + pitch + "\n"; text.text += "roll: " + roll + "\n"; } Some shots where everything just works... I am happy now because i can actually port this to the other project! (I hope)
Ok, so i made a mistake when taking the screenshot, pitch from 22.5 to 135 in unity
And with all that said, thanks to everyone that read and tried to help me out. I never want to have to figure this out again...
That being said, i still would very much appreciate input on what i have done and if it can be simplified in any way as well as perhaps some eureka hints you could send my way to actually make me understand what i have done here.
Cheers!