- Home /
How do I make my player flip to always face the enemy and vice versa?
I'm creating a 2D fighting game and I have the players basic movement down, but nothing happens when the player jumps over the enemy; they both face away from each other. Like in other 2D fighting games, I want both characters to constantly face each other no matter where their position is.
I don't have original code for people to work off of, so I understand if you aren't comfortable sharing your personal code, but if someone could point me in the right direction that'd be great.
Answer by CardboardComputers · Aug 13, 2020 at 09:29 PM
For each of the (presumably two) players, just write something like:
public class FighterPlayerController : MonoBehaviour {
public transform targetTransform;
/* ... */
void Update() {
/* ... */
// This comes out to -1 when the character we're trying to face
// is to the left (right? depends on your camera tbh) of our character
// In other words it just returns 1 for any input >= 0, -1 otherwise
float directionScale = Mathf.Sign(targetTransform.position.x - transform.position.x);
// Then use it here to basically kind of flip the character
transform.scale = new Vector3(directionScale, 1, 1);
/* ... */
}
}
Of course, I'm not sure exactly how your setup looks, and this is just an example, but those few lines in the middle are probably sufficient if you just want the characters to...kind of instantly flip to face each other.
Your answer
Follow this Question
Related Questions
2D game unity player flip problems 1 Answer
2D example: Flip character without moving 0 Answers
2d shooting problem 1 Answer
Rotation problem 1 Answer
Flip the player with arm rotation 2 Answers