- Home /
Changing texture depending on angle to another player
Hi there,
I'm working on a retro "pseudo 3D" game (think original Doom) and it has two play screens, one for each player.
I have worked out how to make each player appear properly in the other player's screen by making sure it always faces the other player's camera (billboard style), so that it doesn't rotate into nothingness...lol.
My question is, I need to change the texture, depending on the angle of the character that the player is looking at. For example... if P1 is in front of P2 and facing away, I want to draw the back texture sprite for P1, and if P1 is facing P2, draw the front. I also have sprites for side on and 45 degree angles (8 sprites in all).
What's the best way to do this? As players can move in 3 dimensions and rotate, it's a bit of a tricky one for a noob like me...
Any help greatly appreciated ;-)
I should add, I can code... not asking for a complete script here... just the best method... I do SUC$$anonymous$$ at math!!! lol. Any pseudo-code, ideas welcome :)
Answer by ckfinite · Aug 28, 2011 at 01:23 AM
Well, the high-level idea is this: get the direction vector, use ArcTangent to find the angle to the the other player, then fetch the correct sprite. Example (C#) code (WARNING: completely untested):
Vector3 direction_vector = player1.transform.position - player2.transform.position;
float angle = Mathf.Atan2(direction_vector.x,direction_vector.z) * Mathf.Rad2Deg;
if (angle > 0 && angle <= 90)
DrawRightSprite();
else if (angle > 90 && angle <= 180)
DrawBackSprite();
//etc...
Awesome, thanks a lot! Should be able to make that work ;-)