[NMS] Check if custom entity is looking directly (Or a little off) at a player

Discussion in 'Plugin Development' started by Dudemister1999, Sep 27, 2014.

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

    Dudemister1999

    Hello! I've got a zombie, and his AI is perfect except for one thing: He will target you no matter where you are relative to him. Walls are the only exception. Can you point me in the right direction on how to accomplish what I need? All I need is a custom PathfinderGoal (Or other) to check if the entity is staring in a player's general direction.
     
  2. Offline

    fireblast709

    This can be done by using the dot product of Vectors. You need two vectors: one is the direction Vector from the entity that we want to check, and the other one is the Vector from that entity to the entity it's supposed to be looking at. Normalize both Vectors and take the dot product: this will give you a value between -1 and 1. The closer it's to 1, the 'more directly' is the entity looking at the other entity.
    Code:
    function isTargeting(Entity entity, Entity other)
    {
        // The first Vector
        Vector dir = entity.getDirection();
        // The second Vector
        Vector towards = other.getLocation().subtract(entity.getLocation());
        // Then normalize both, and take the dot product between both
        double dot = dir.normalize().dot(towards.normalize());
        // 0.75 is just an assumed number. The closer to 1, the more precise your check will be
        return dot > 0.75;
    }
     
  3. Offline

    Dudemister1999

    fireblast709 Thank you! I'll try it out, and get back to you on its results.
     
Thread Status:
Not open for further replies.

Share This Page