How to make object aim in direction of rotation.

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);
	}

test comment.

yup 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

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

I am trying to find the answer in all of that, don't fully understand how it will help me just yet...

If you want to reply to someone please use the comments and not the answers next time.

I converted your answer to a comment. If you want to reply to an answer please use the comments next time.

Has anyone else got the problem of not being able to use the "send message" button in the moderation qeue?

These are 3 different ways to make the object keep looking in the direction it's moving. The first one is a fix to your code: it makes the object look at target.position with a certain delay due to Slerp. The second alternative does the same thing, but without any delay because Slerp isn't used anymore. Finally, the 3rd alternative makes the object look in the direction it's going to - it's good for arbitrary paths, and works fine when the movement is a few degrees per <strike>second</strike> frame (say, 0.5 to 5 degrees/<strike>second</strike> frame).