Quaternion.LookRotation issues

I’m having issues using Quaternion.LookRotation. I’m trying to get object A to always move toward object B. Here is the code I’m using:

	targetRotation = Quaternion.LookRotation (myAttackTarget.position - transform.position);
   str = Mathf.Min (RotateSpeed* Time.deltaTime, 1);
   transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation,  str);

If object A has object B in the +X axis (in front of it), everything works fine. Object A moves toward object B with no problem.

The problem starts when object A has object B in the -X axis (behind it). Then object A does it’s best to move away from object A. What can I do to always make sure object A heads toward object B?

I can’t quite say why you are getting the results you do, but LookRotation assumes that +Z is forward, not +X. Also, you might try to use Slerp instead of Lerp.

Rune

thanks for the reply.

Did I mention this was for a 2D game?

What’s the difference between Lerp and Slerp?

No. If you only want the rotation to happen in a single plane (for example the X-Z plane), you should not be using Slerp or Lerp, as it generally gives you no control over which plane the rotation happens in.

In that case, do a search for “turning” and maybe “angle” - there are quite a few threads on the subject.

However, if an object rotation in 3D is fine even for your 2D game, then Slerp should be good.

Slerp is spherical linear interpolation - it ensures that the rotation angle changes linearly with the interpolation value. Lerp does not ensure this, especially not for large angles.

Rune

one other thing I should mention is that I’m using a configurable joint to restrict motion to the XY axis. I’m wondering if that is where my issues are coming from?

That probably depends on whether you are restricting the angular motion or only the translation.

Did you try to follow the other suggestions (using Z as the object’s forward, and using Slerp instead of Lerp)? Does it not have any effect?

Rune

Yeah. I tried both of those…still had the same issue. not sure why it’s doing it.

I’ve decided that it may be desirable behavior for this particular game, and am going to leave it for the moment…