# [SOLVED]Player Direction

Discussion in 'Plugin Development' started by thefiscster510, Apr 27, 2012.

Not open for further replies.
1. Offline

### thefiscster510

Hey, So all I want is to know how to get the direction the player is facing so that I can invert it... Please help if you can that'd be great! Thanks!

#1
2. Offline

### evilmidget38

Player.getLocation().getDirection() should probably do what you want.

#2
PureGero likes this.
3. Offline

### thefiscster510

Well, I want something that I could invert and then convert to a BlockFace..
Like, If they're pointing South West then I want to make a block face that's "BlockFace.NORTH_EAST;" i think...

#3
4. Offline

#4
5. Offline

### thefiscster510

Thanks anyway. And for anyone else that happens to see this, I want this because i'm placing a sign with a command and i want the sign to be facing the person who placed it... So, somehow getting this out of where the player is facing would work to... Just any method so long as it works.

#5
6. Offline

### Iron_Crystal

Well, this would take some trial and error, as I do not know the directions. What you would do is find a block like x + 1 from the player. Then make a command or something that makes the player face that direction. Then press f3 and see what direction you are facing (I believe south = 0, west = 1, north =2, and east = 4). Then record what direction x + 1 made you turn. If it makes you face north, then x - 1 is south. Then try z + 1. If z + 1 makes you turn west, then z - 1 is east. If you want things like north east, then you will have to face the block z + 1 x + 1 or whatever it is. I do not know the directions at the top of my head so these are total guesses.

#6
7. Offline

### thefiscster510

@Iron_Crystal
Ok, So doing what i THINK you said, I have discovered that Z axis is North/South. Z+ = South, Z- = North. And X Axis is East/West, Z+ = East, Z- = West.

Also, 0 = W, 1 = N, 2 = E, 3 = S
So, how would i get their current direction from that?

#7
8. Offline

### Iron_Crystal

Well, maybe while they are doing whatever they do that places the sign, you can get the target block with player.getTargetBlock(null, 100) and get the location and see what the coordinates are relative to the player.

#8
9. Offline

### nicholasntp

Code:java
` public static String getCardinalDirection(Player player) {        double rotation = (player.getLocation().getYaw() - 90) % 360;        if (rotation < 0) {            rotation += 360.0;        }         if (0 <= rotation && rotation < 22.5) {            return "N";        } else if (22.5 <= rotation && rotation < 67.5) {            return "NE";        } else if (67.5 <= rotation && rotation < 112.5) {            return "E";        } else if (112.5 <= rotation && rotation < 157.5) {            return "SE";        } else if (157.5 <= rotation && rotation < 202.5) {            return "S";        } else if (202.5 <= rotation && rotation < 247.5) {            return "SW";        } else if (247.5 <= rotation && rotation < 292.5) {            return "W";        } else if (292.5 <= rotation && rotation < 337.5) {            return "NW";        } else if (337.5 <= rotation && rotation < 360.0) {            return "N";        } else {            return null;        }    } `

This returns the player's direction. I got some of this from one of sk89q 's plugins.

#9
10. Offline

### Iron_Crystal

Thanks for this! I am definitly saving this code!

#10
11. Offline

### nicholasntp

No problem! Happy to help! Thank sk89q for the math stuff. I used this for playing smoke effects.

#11
12. Offline

13. Offline

### nicholasntp

You're very welcome!

Did it work out for you?

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 25, 2016
#13
14. Offline

### thefiscster510

@nicholasntp
I did =3 But i addapted it to my needs. Just made this function.

Code:
``` public Byte direction(Player player){
double rotation = (player.getLocation().getYaw() - 90) % 360;
if (rotation < 0) {
rotation += 360.0;
}
if (0 <= rotation && rotation < 22.5) {
return 0xC; //S > E
} else if (22.5 <= rotation && rotation < 67.5) {
return 0xE; //SW > SE
} else if (67.5 <= rotation && rotation < 112.5) {
return 0x0; //W > E
} else if (112.5 <= rotation && rotation < 157.5) {
return 0x2; //NW > SW
} else if (157.5 <= rotation && rotation < 202.5) {
return 0x4; //N > W
} else if (202.5 <= rotation && rotation < 247.5) {
return 0x6; //NE > NW
} else if (247.5 <= rotation && rotation < 292.5) {
return 0x8; //E > N
} else if (292.5 <= rotation && rotation < 337.5) {
return 0xA; //SE > NE
} else if (337.5 <= rotation && rotation < 360.0) {
return 0xC; //S > E
} else {
return null;
}
}```
I actually needed it for a sign placement thingy. Thanks for all of your help gais!

TwixGamingYT likes this.
15. Offline

### nicholasntp

Lol ok! No problem!

#15