- Home /
Rotate object to face up from sphere
I need to rotate an object so that it is always "up" from a sphere like it would be on a planetary surface. I have tried using Quaternion.LookRotation but as you would guess this just makes the object face the ground. Is there an easy way to go about calculating the rotation of the object so it is always up relative to a point on a sphere?
Answer by robertbu · Jul 27, 2013 at 10:33 PM
Try adding this logic to your object:
var sphere : Transform;
function Update() {
var v3 = transform.position - sphere.position;
transform.rotation = Quaternion.ToFrom(transform.up, v3) * transform.rotation;
}
I'm currently on Unity 5.5.0f3 and the updated version of
transform.rotation = Quaternion.ToFrom(transform.up, v3) * transform.rotation;
is now
transform.rotation = Quaternion.FromToRotation(transform.up, v3) * transform.rotation;
Your answer
Follow this Question
Related Questions
Angle between two vectors rotated by quaternion? 0 Answers
How do you make a sphere from 6 planes? 1 Answer
how to know when rotation is completed ? 2d 4 Answers
Flip over an object (smooth transition) 3 Answers
How to attach sphere to rotating cube? 0 Answers