How I can make a entity despawn if player looks

Started by Igor_santuchi on

Topic category: Help with modding (Java Edition)

Last seen on 18:32, 25. Jul 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How I can make a entity despawn if player looks
I'm trying to make an entity that disappears when the player looks at it
Pls Help
Last seen on 05:04, 30. Jul 2024
Joined Jan 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
In one of my mods, I execute…
Wed, 07/10/2024 - 21:53

In one of my mods, I execute a command that gives a status effect to certain kinds of entities the player looks at.

Such as: /execute as @e[type=minecraft:villager] at @a[distance=..12] facing entity @s feet positioned ^ ^ ^1 rotated as @a[limit=1] positioned ^ ^ ^-1 if entity @a[distance=..0.1] run effect give @s minecraft:slowness 1 255 true

"[distance=..12]" controls how far away the command works from. The way it's written here allows it to work within 12 blocks. If you look at the entity from further than 12 blocks away, nothing will happen.

"[distance=..0.1]" controls how directly you have to be looking at the entity. If you imagine a bubble around the entity's eyes, and say the game thinks you're looking at the entity if you're looking at that bubble, this number controls the size of that bubble.

You could either give them a specific effect like unluck 255 and then constantly check to see if your entity has unluck 255 or not and despawn them if they do. Or, instead of giving them a status effect, you can try teleporting them into the void.

Last seen on 18:32, 25. Jul 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
you have a image?
Thu, 07/11/2024 - 16:45

you have a image?

Last seen on 05:04, 30. Jul 2024
Joined Jan 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I suggested executing a…
Thu, 07/11/2024 - 18:31

I suggested executing a command- which should be done every tick.
You can have an on tick procedure on the entity itself that executes a command at the location of the entity.

Then you can check for the status effect using the same procedure in an if statement and then despawn the mob if it detects that it has the effect with the correct amplifier.


As for Ousieks' suggestion, I haven't tested it, but that looks to me like it would despawn the entity as long as you were both facing the same direction. It isn't able to check if you were looking at the entity specifically.