Pitch And Yaw

Discussion in 'Plugin Development' started by zakarls, Jun 21, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    zakarls

    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 :)
     
  2. Offline

    Deleted user

    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
    1.  
    2. public boolean timerStarted = false;
    3. public int timerID = 0;
    4.  
    5. public void startTimer() {
    6. new BukkitRunnable() {
    7. public void run() {
    8. timerStarted = true;
    9. timerID = this.getTaskId();
    10.  
    11. for (Player player : Bukkit.getServer().getOnlinePlayers())
    12. ZombieHandler.checkForTarget(player);
    13. }
    14. }.runTaskTimer(plugin, 0L, 10L);
    15. }
    16.  
    17. public void stopTimer() {
    18. if (timerStarted)
    19. Bukkit.getServer().getScheduler().cancelTask(timerID);
    20. }
    21.  


    And here is my checkForTarget() method.

    Code:java
    1.  
    2. public static void checkForTarget(Player player) {
    3. if (player.getGameMode() == GameMode.CREATIVE)
    4. return;
    5. for (Entity e : player.getNearbyEntities(45, 45, 45)) {
    6. if (!(e instanceof LivingEntity))
    7. return;
    8. if (!(e instanceof Zombie))
    9. return;
    10. Zombie zombie = (Zombie) e;
    11.  
    12. if (zombie.getTarget() != null)
    13. return;
    14. zombie.setTarget(player);
    15. }
    16. }
    17.  


    This particular method increases Zombie's range to view players and target them up to 45 blocks away. (x, y, z)
     
  3. Offline

    Flamedek

    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)
     
  4. Offline

    zakarls

    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 :p sorry for asking.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    Deleted user

    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..
     
  6. Offline

    zakarls

    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
     
Thread Status:
Not open for further replies.

Share This Page