# help with rotation

Hi, so here’s the deal, my charater is mowing to the player and i want him to totate towards the player only in to ways 90 degrees if the player is in front of the character and -90 degrees if player is behind the character so character is always facing the player (like in good old NES beat’em ups games). Here’s my solution which is not working…:

``````            Vector3 moveVector = target - transform.position;
moveVector.y -= gravity * Time.deltaTime*10f;
character.Move(moveVector *speed *Time.deltaTime);
Vector3 directionToTarget = this.transform.position - target;
float angel = Vector3.Angle(transform.forward, directionToTarget);
if (Mathf.Abs(angel) > 90  Mathf.Abs(angel) < 270){
newRot.eulerAngles = new Vector3(0, 90, 0);
this.transform.rotation = newRot;
}
if (Mathf.Abs(angel) < 90 || Mathf.Abs(angel) > 270){
newRot.eulerAngles = new Vector3(0,270, 0);
this.transform.rotation = newRot;
}
``````

primitive solution:

``````       Vector3 moveVector = target - transform.position;
moveVector.y -= gravity * Time.deltaTime*10f;
character.Move(moveVector *speed *Time.deltaTime);
Vector3 directionToTarget = this.transform.position - target;
float angel = Vector3.Angle(transform.forward, directionToTarget);
if (Mathf.Abs(angel) < 90 || Mathf.Abs(angel) > 270){
seeU = false;
}
if (Mathf.Abs(angel) > 90  Mathf.Abs(angel) < 270){
seeU = true;
}
if(!seeU){
transform.Rotate(0,180,0);
}
``````

something like this probably works as well.

``````Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = target - transform.position;
if (Vector3.Dot(forward, toOther) < 0)
transform.Rotate(0,180,0);
``````

yep, that’s just perfect optimisation))) thnx!

Assuming my knowledge of scripts is accurate (lol - its a bit so so)

this probably works without unnecessary allocations…

``````if(Vector3.Dot(transform.forward, target - transform.position) < 0)
transform.Rotate(0,180,0);
``````