- Home /
Make an object point to another which keeping its forward along an axis
Hi there,
I have some handy execute in edit mode scripts I wrote in order to make level design easier. One of the big things I wanted is to be able to point an object's bottom to be at the same orientation as the floor beneath it - in other words, its transform.up should be pointing in the same direction as the normal at the point below it.
So if I put a chair on ground that was slanted 15º, then the chair would also be slanted 15º. Make sense?
I have this implemented by doing a raycast downwards, if it hits anything, moving the object there and setting transform.up = raycastHit.normal (also have tried raycastHit.collider.transform.up). This works for aligning the object, but unfortunately also rotates the object around (its forward rotation is changed too).
Is there some proper way to do this?
This would be totally doable if there were a way to get the rotation around my transform.up. That way, I could store the rotation around transform.up (would be a single float) then use transform.Rotate to rotate around my new up that same amount. But I see no way to get the rotation around an axis unless it's a global axis.
I appear to have found a solution:
transform.rotation = Quaternion.LookRotation(transform.forward, matchUpOfObject.transform.up);
The above for some reason does absolutely nothing. When I changed it to the very hokey:
transform.rotation = Quaternion.LookRotation(transform.right, matchUpOfObject.transform.up); transform.rotation = Quaternion.LookRotation(-transform.right, matchUpOfObject.transform.up);
It appears to work. Very weird to me, but whatever...
Your answer
Follow this Question
Related Questions
Look at like rotating around y axis 1 Answer
ConfigurableJoint - angular positions (rotation) problem 1 Answer
Make Quaternion affected by float 1 Answer
Rotation of an object with quaternion 1 Answer
Reverse rotations problems... 2 Answers