# Rotate and move toward a target

hit all~~
I have the following code

`````` void testMovement(){

Vector3 relativePoint = mechModel.InverseTransformPoint(target.position);
Quaternion newRot = Quaternion.LookRotation(new Vector3(relativePoint.x,0f,relativePoint.z));
mechModel.rotation = Quaternion.Slerp(mechModel.rotation,newRot,Time.deltaTime*movingSpeed);

mechModel.Translate(Vector3.forward*movingSpeed*Time.deltaTime);
}
``````

target is a sphere I placed in the test scene.

and in my project , when I click on ground it will spawn a character on where I click. And it will find a target. then the above function will kick in. It all work well when I spawn the character in front of the target. It will rotate and move towards target.
However when I spawn the character behind the target, my character will only rotate 90 degrees and stop rotating. As you can see
moving is a simple Translate. I couldn’t figure out why it only rotates 90 degrees. Can any one shed some light ?

I think you are mixing up local space and world space in your method. relativePoint and newRot are in local space, but then you are applying the newRot to mechModel.rotation, which is in world space.

Might be easier to imagine this entirely in world space:

``````Vector targetDirection = target.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(new Vector3(targetDirection.x, 0, targetDirection.z));
mechModel.rotation = Quaternion.Slerp(mechModel.rotation, targetRotation, Time.deltaTime * movingSpeed);

mechModel.transform.position += mechModel.transform.forward * movingSpeed * Time.deltaTime;
``````

hi~~thanks for the help…
I tried the code you provided, but it still behave more or less the same.
I am guessing I would probably have to do better testing on the position of my target (ie left / right /front/ back) and do the rotation accordingly.

Thats weird, it is some pretty simple and straightforward code, I’ve used it a thousand times to make characters follow targets, so I bet my nuts the problem must be somewhere else

I found it strange as well, I used the same code for turret and it works.
Now I put the same code on my character, and it doesn’t behave as expected.
The different between character and turret is that turret is stationary, and target is moving. And in character’s case, character is moving and target is not
(well…in this case anyway)

UPDATE:
I decided to start out fresh, and now the code is now working as expected…thank you.