- Home /
Sprite facing wrong direction
I'm making a 2d top down shooter and using this code to get the enemy to face the player
void facePlayer(){
vectorToTarget = Player.transform.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 10);
}
However, the sprite is not facing the right direction. He faces my character with his left arm instead of his head. https://imgur.com/a/4cH5fkP
How can I set the sprite's direction?
Comment
Answer by SalahChafai160 · Apr 04, 2019 at 11:45 AM
this should get it to face the player void facePlayer(){ vectorToTarget = Player.transform.position - transform.position; float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) Mathf.Rad2Deg - 90; Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward); transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime 10); }