Make mob only chase players with low health

Started by WordedPuppet on

Topic category: Help with modding (Java Edition)

Last seen on 01:11, 23. Oct 2023
Joined Oct 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Make mob only chase players with low health

The idea is that the mob will run away from the player if they have more than half HP, but anything less and they'll chase them down.
This is the code I have that's meant to make the mob run away from players with more HP, but it does not compile.
Any help would be very appreciated.

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
The problem is that it will…
Thu, 10/19/2023 - 01:51

The problem is that it will check for this regardless of whether there's actually a player within a 64 block radius. If there isn't it'll return null and crash the game. (So you need to nest the first section in another 'if' bracket that checks if a player exists in a 64 block radius.) Also, the condition for fleeing probably doesn't provide x/y/z coordinates as a dependency, it'll just give you an entity. (So you need to replace x/y/z with x position of entity, y position of entity, and z position of entity.) 

That being said, still not sure if this will work. You could try making the entity neutral and then only have it set its attack target to the player if there's a player nearby with low health, if you're willing to change how this works a bit.

Last seen on 01:11, 23. Oct 2023
Joined Oct 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank you for your advice. I…
Thu, 10/19/2023 - 03:26

Thank you for your advice. I was able to get it to work by storing the player's position in a local variable first, instead of trying to read it directly.