Get player being looked at

Discussion in 'Plugin Development' started by AbeJ, Aug 18, 2011.

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

    AbeJ

    I'm creating a pretty unique (I think) magic plugin for Bukkit. I need to get the player being looked at by the spell-caster. Is this possible?
     
  2. Offline

    nisovin

  3. Offline

    Taco

    Without looking at nisovin's code, I'd get the location of where the player's looking and the entities near that location within a certain tolerance. He may have a better solution though.
     
  4. Offline

    bergerkiller

    Totally different approach here. :p
    I'd calculate the look-at yaw and pitch values for all entities and pick the entity that has the lowest angle difference with the player.

    Code:
        public static float getAngleDifference(float angle1, float angle2) {
            float difference = angle1 - angle2;
            while (difference < -180) difference += 360;
            while (difference > 180) difference -= 360;
            return Math.abs(difference);
        }
     
  5. Offline

    AbeJ

    Thanks all. Noviso's code worked great, I'll also try Bergerkiller's one. In the end, I'm going to use a completely different approach: spawning particles along the path of the spell, and targeting the first entity or block that gets hit. That way, you can visually see the path.
     
  6. Offline

    EdTheLoon

    @AbeJ bergerkiller's solution is quite accurate but it doesn't account for the distance the targeted entity is from the player. Taco's solution accounts for distance but may not be accurate in returning the closest target. You're best solution would be a good balance of the two.
     
  7. Offline

    bergerkiller

    Yup, true. Mine would return the player in a cave when you looked down...lol :p
    Good luck with adding a custom moving entity spawning 'smoke?' while moving along. If you need help with velocity/location update algorithms, just ask. :)
     
Thread Status:
Not open for further replies.

Share This Page