how to get nearest entity to an entity

Started by carnivowrist on

Topic category: Help with Minecraft modding (Java Edition)

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how to get nearest entity to an entity

I'm trying to make a procedure in which each tick (or every X ticks) it will set the attack target of an entity to the nearest entity to it, but if i use "get nearest entity" it will select itself, and so if i make it the attack target, the entity attacks itself as it considers itself the nearest. I tried other things like "for each entity as 'entity iterator' at X Y Z" --> set the attack target to entity iterator, which works well but this sets the attack target of the entity to the furthest entity to it.. i have no solutions

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
okay then try, if (event…
Sun, 08/31/2025 - 06:58

okay then try,

if (event instanceof LivingIncomingDamageEvent _event) {

_event.setInvulnerabilityTicks(0);
}
Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
im sorry but it gave me an…
Sun, 08/31/2025 - 22:56

im sorry but it gave me an error when saving the procedure.. do i have to change anything in the code?

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
huh what's the error
Mon, 09/01/2025 - 07:16

huh what's the error

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh it looks like this might…
Mon, 09/01/2025 - 12:55

oh it looks like this might be harder in older version,

can you show me the generated code for the trigger?

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm really not sure but try…
Mon, 09/01/2025 - 14:40

I'm really not sure but try this,

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
it works well! Now the…
Mon, 09/01/2025 - 17:07

it works well! Now the problem is that it's messing with custom attacks for my entities

im sorry if im getting annoying lol but i can't change the invulnerability time, it's either 0 or forever if i could change it and have it set for example just 1 tick instead of 0 it would fix it

i also can't set it just for specific entities (like i tried adding 'if source entity= my entity')

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
huh you can't do it with…
Mon, 09/01/2025 - 17:13

huh you can't do it with this block?

and umm I'm really not sure why it would be making the invulnerability last forever...

ig you could try like,

 

oh /*code*/ was supposed to be return;

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
okay well i can actually do…
Mon, 09/01/2025 - 17:41

okay well i can actually do it for specific entities like in your screenshot but i mean that if i change the number in the code snippet 'entity.invulnerableTime = 0;' to no matter what it seems to stay 0 and past a very big number (i tried most from 0 to 100) it glitches and entity is invincible (and red damage filter on entity doesn't appear)

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh um did you try the wait 1…
Mon, 09/01/2025 - 17:43

oh um did you try the wait 1 ticks thing?

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
i tried it also didn't work
Mon, 09/01/2025 - 17:52

i tried it also didn't work

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh, dam, sorry I really don…
Mon, 09/01/2025 - 18:15

oh, dam, sorry I really don't know why that's happening or how to make a workaround..

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
ok then the reason i can't…
Mon, 09/01/2025 - 23:02

ok then the reason i can't let it to be 0 is here: https://ibb.co/WWyC7wWs

basically i have a procedure which replaces the normal attack of an entity: it cancels the attack, plays an animation, and after a couple ticks it does a hit sound and do damage to the target

in the 'for each entity as entity iterator' i have it set up like this to check if the target of the entity is too far for attack or not, but the problem is that sometimes it does it double very randomly, plays the sound and damage two times, so if the invulnerable time is 0 it basically doubles the damage..

i need a way to make it so it checks the distance between an entity and another, correctly

if you don't know ill make a forum