Entity Targeting

Started by znibsss on

Topic category: Help with modding (Java Edition)

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Entity Targeting

Hello, and it is me again! This time, with another question. How can I make my creature, znibsss, target a player even if the player is behind a wall? Or even if its far away? How can I make it so that my creature will ALWAYS be targeting a player? Yeah that's all I'm gonna ask, for now.

Last seen on 11:45, 7. Oct 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You can make a procedure…
Tue, 11/07/2023 - 05:11

You can make a procedure that runs on your entity's update tick, and checks if a player exists in a desired radius. (Say, 200 blocks, to be generous.) If a player does exist in a 200 block radius, set the attack target of the event/target entity to the nearest player in a 200 block radius.

Both of these functions ('does entity exist in radius' and 'nearest entity of type in radius') can be found in the world menu. It is very important that you make sure there is an entity within the radius before you set the attack target, otherwise nearest entity can return a null entity, which will immediately crash the game. 

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank you! Also another…
Sun, 11/12/2023 - 19:17

Thank you! Also another question, how can I make it target a player as I said only if it is not already targetting something? Like not just if it is not already targetting a player, a wolf also maybe a skeleton etc.

Last seen on 11:45, 7. Oct 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This is actually the default…
Mon, 11/13/2023 - 02:10

This is actually the default; once an entity's attack target is set, it will only choose a different attack target if it is somehow reset. (Either by the original target dying/leaving range, or if the new target deals damage to an entity that has a 'fight attacker mobs back' function.) If you need to specifically stop it from targeting an entity, there are other means to do this, though they're a bit more complicated.

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This isn't the default? When…
Wed, 11/22/2023 - 13:59

This isn't the default? When I did exactly that it no longer attacked the attackers and it stopped attacking whatever it was attacking and started attacking me.

Last seen on 11:45, 7. Oct 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It's highly dependant on the…
Wed, 11/22/2023 - 14:17

It's highly dependant on the order of the tasks in the Mob AI. If it's still causing problems, or you need a more complicated system, another thing you can do is forget about attack AI tasks entirely; and just use the mob's update tick trigger to run your own custom procedure that determines what the entity should be targeting, if anything. (The 'does entity exist in radius' 'nearest entity of type' and 'set attack target of entity' functions should all be helpful for this.)

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Ok how can I make it so only…
Wed, 11/22/2023 - 16:36

Ok how can I make it so only if my entity isn't targetting anything it will target the player?

Last seen on 11:45, 7. Oct 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Use an if bracket to check…
Wed, 11/22/2023 - 16:40

Use an if bracket to check that the 'entity (event/target entity) is targeting for attack' is not a subtype of living entity. (Since your mob won't be targeting non-living entities, this will only return true if it has no attack target.)

If the entity is not targeting anything, use the 'does entity exist in radius' function to check if a player exists in your desired radius. If this returns true, use the 'set attack target of entity' function to target the nearest player. (Using the 'nearest entity of type' function.)

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can you send me screenshot…
Wed, 11/22/2023 - 17:19

Can you send me screenshot of you doing this in a test mod because I cannot quite understand what your telling me

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Ok I just did what you did,…
Wed, 11/22/2023 - 17:37

Ok I just did what you did, now ill make another procedure with entity attacked and I'll try to figure something out. What I'll try to do is just attack the attacker and maybe that'll work

Last seen on 11:45, 7. Oct 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
The top procedure is an…
Wed, 11/22/2023 - 17:46

The top procedure is an example of what you would do for the 'update tick procedure. (First checking that the 

The lower procedure is something you could do using the global 'before entity hurt' procedure, (first checking that the event/target entity is your custom entity.) The way this is configured, the entity will only fight its attacker back if it isn't already targeting an entity. If you remove the if bracket and just use the inner block, it will change its attack target to whatever attacked it last.

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Oh well, it worked! Thank…
Wed, 11/22/2023 - 17:50

Oh well, it worked! Thank you man you're a legend!

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Also the procedure before…
Wed, 11/22/2023 - 17:53

Also the procedure before entity hurt is like when an entity is targetting an entity? Or it is when he is gonna receive the damage? Just so I can get some more knowledge.

Last seen on 11:45, 7. Oct 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
‘Before entity is hurt’…
Wed, 11/22/2023 - 17:57

‘Before entity is hurt’ triggers right before the game calculates damage; which is useful, because you can use the entity, the source entity, damage type, and the amount of damage as dependencies; and can also cancel the trigger. It also works if there is no source entity, so you can use it for any sort of damage. (Though you may still need to check if the source entity exists, I’m not 100% sure)

Last seen on 23:55, 1. Aug 2024
Joined Dec 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Oh nice thank you :)
Wed, 11/22/2023 - 18:06

Oh nice thank you :)