How would I make it so when a player that is in a hashmap moves, the nearby entities will look towards them. I know it has something to do with pitch and yaw but thats about it. Thanks for any help
Pitch and Yaw are the direction (like x and y) a player is looking. A player's Location consist of; world,x,y,z,pitch,yaw. To do this some may suggest using the PlayerMoveEvent however I believe this is an awful suggestion because the even fires anytime a player moves (even just adjusting pitch / yaw) which can use a lot of resources and cause lag. So.. What I would suggest is creating a timer and when the timer runs loop through online player's and check to see if they've moved (put the player's Location in a Map and each loop check to see if their previous Location is different from their current one). I'm currently in class (in the middle of a lecture) and I don't have the time to explain how to do all this so I'll just provide you with some sample code. Code:java public boolean timerStarted = false;public int timerID = 0; public void startTimer() {new BukkitRunnable() {public void run() {timerStarted = true;timerID = this.getTaskId(); for (Player player : Bukkit.getServer().getOnlinePlayers())ZombieHandler.checkForTarget(player);}}.runTaskTimer(plugin, 0L, 10L);} public void stopTimer() {if (timerStarted)Bukkit.getServer().getScheduler().cancelTask(timerID);} And here is my checkForTarget() method. Code:java public static void checkForTarget(Player player) {if (player.getGameMode() == GameMode.CREATIVE)return;for (Entity e : player.getNearbyEntities(45, 45, 45)) {if (!(e instanceof LivingEntity))return;if (!(e instanceof Zombie))return;Zombie zombie = (Zombie) e; if (zombie.getTarget() != null)return;zombie.setTarget(player);}} This particular method increases Zombie's range to view players and target them up to 45 blocks away. (x, y, z)
Eballer48 Does setTarget() even work properly? I found it works on zombies, and only zombies, but the maximum range it works at is about 30-35 blocks. (they might still have you as target beyond that but not start moving to about that range)
Eballer48 and Flamedek When I use ZombieHandler it doesnt seem to work. Is this from another library that I am not using? Oooooh. I see. ZombieHandler is a different class sorry for asking. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
From my experience, I have no problems using setTarget() although I've only tested with Wolves and Zombies. Also I've noticed it having no range limit in an open field, however when I was originally testing I made a "air strip" type test zone and for some reason it didn't work. However I then created a circular arena for testing this method and found it worked fantastically, and I haven't found any problems with it glitching out in an open world. EDIT: btw zombie's default range is 35 blocks so..
Eballer48 Thank you very much but this is kind of not what I was asking. I was asking if you can set the pitch and yaw of nearby entities to look at a specific player. Like I could do /look and it would put them in a hashmap. Then on a timer it would check if theyve moved and set nearby entities pitch and yaw to look at them