- Home /
How to rotate an object to align with the scenery
I'm struggling to articulate this question, so please bear with me.
Imagine a skateboard. When rolling down the road, the deck of the skateboard rotated so as to align with the road, albeit a few inches higher. You might also say, it has no rotation.
When he skateboard is on a ramp at a 45º incline, the deck is rotated to align with the ramp. Perhaps the rotation along the x axis is 45º.
But, if the skateboard is approaching the ramp at an angle, the rotation of the board is aligned with the ramp, but the decks rotation along each axis will not be 45º. So one way of phrasing this question might be, how do I calculate the rotation of the deck.
Here's an attempt at visualising the problem. Take the points at the top of each purple pole.
Now imagine I want to place a the green box in such a way that it appears to align naturally with each of those four points.
.
If I know those four points, how would I calculate the rotation for the green box?
Answer by Digger8048 · Jan 21, 2020 at 10:41 PM
you can use the normal of the plane described by three of the four points you have.
Vector3 dir1 = (point2 - point1).normalized;
Vector3 dir2 = (point3 - point1).normalized;
Vector3 normal = Vector3.Cross(dir1, dir2);
greenBox.position = (point1 + point2 + point3) /3;
greenBox.rotation = Quaternion.LookRotation (normal);
Your answer
Follow this Question
Related Questions
Rotate an object so its up vector matches a sphere 1 Answer
Unable to fully rotate GameObject on tap 2 Answers
VR: Controller rotation delta along other axis 2 Answers
Rotate a Vector3 direction 3 Answers