Jump to content

Checking if a mob is in front of another?


AXELTOPOLINO

Recommended Posts

Hello guys,

I've just made a new mob and I'd like to check if there's any other mob in front of it. If NOT my mob should throw an arrow, otherwise it shouldn't do that (so it shouldn't damage its "allies"). I'm actually creating an invisible entity for this which collides with any possible mob and check if my mob can shoot, but it wastes a lot of useful memory and packets. Is there any other way for doing that? Like for ex. a ray tracing way (I don't know what should I look for in order to learn about ray tracing)?

 

Thanks and have a good day/night,

Axel

Link to comment
Share on other sites

Hi

 

I'd suggest:

(1) search for entities nearby.  Discard any which are not nearby.

(2) For each nearby entity, check whether its bounding box is intersected by the line of fire of the arrow (i.e. the bounding box has four vertical edges: if they are all to the left of your line of fire, or all to the right, then the arrow will miss.  Otherwise it may hit.

(3) optional: ray trace from the target entity to your mob, to see if there are any blocks in the way which would shield the entity from the arrow.

 

I probably wouldn't bother with the ray trace myself, I suspect that in most cases it wouldn't actually make any difference and it's likely to be rather expensive to compute.

 

Figuring out "left or right" requires a bit of vector math, not too hard if you've studied math, but let us know if you need a pointer in the right direction.

 

Alternatively - you could just make your mob immune to its own arrows.  Would be an awful lot easier.

 

-TGG

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.