I have an object in my scene that rotates around a center point (pivot).
When I rotate the object, I want it to turn and follow the path, like a person running in a circle around a center point. Like this picture
. The code I have so far makes the object look at the center point constantly.
How would I make it turn and aim in the path that it is moving??
Here’s what I have so far
else
{
myTransform.RotateAround(pivot.position, Vector3.up, rotateAroundSpeed * Time.deltaTime);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
1 Answer
1
RotateAround keeps the object looking at the center all the time. Since it recalculates the object rotation each frame, Quaternion.Slerp doesn’t accumulate over time - it’s always trying to deviate the newly calculated rotation by rotationSpeed*Time.deltaTime, which’s a very small number.
In order to use Slerp, you should save the object rotation before RotateAround:
var curRot = myTransform.rotation; // save the current rotation
myTransform.RotateAround(pivot.position, Vector3.up, rotateAroundSpeed * Time.deltaTime);
// use it in Slerp:
myTransform.rotation = Quaternion.Slerp(curRot,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
The object will always be looking a little outside, since Slerp can’t follow the orbit instantly. You could instead directly set myTransform.rotation without any Slerp, like this:
myTransform.rotation = Quaternion.LookRotation(target.position - myTransform.position);
A more versatile solution would be to calculate the new direction according to the last movement:
var lastPos: Vector3; // declare this variable outside any function!
...
myTransform.rotation = Quaternion.LookRotation(myTransform.position - lastPos);
lastPos = myTransform.position; // update last position
test comment.
– ByteSheepyup just take the cross product of towards-the-middle and up. by "up" I mean the "axle" of the wheel that turning. so if the wheel is turning flat on the surface of the earth, I mean up-up. there are many good cross product questions on here, ad you should familiarise yourself with it (wikipedia) for game programming http://answers.unity3d.com/questions/338537/great-circle-trajectories-around-a-sphere.html http://answers.unity3d.com/questions/266972/detecting-mesh-orientation.html note the extremely important diagrams
– Fattie