I have two entities and I want to see if the second entity is x+
or x-
to the first entity. I want to do this for all three axis. Anyone know how I can do this? In addition to this, I've tried execute @e[type=Bat,name=Coord1] ~ ~ ~ testfor @e[x=~,y=~,z=~,type=Bat,name=Coord2]
to see if the entities are at the same location but it doesn't seem to work.
Minecraft checking entity positions
minecraft-java-edition
Related Topic
- Minecraft execute tellraw’s clickEvent from original entity, not player
- Minecraft – How to test for a mob, with a given name, in a certain location
- Minecraft – My command for minecraft executing a command at an entity won’t work
- Minecraft – How to test for the entity closest to the player
- Minecraft – Bats don’t drop items they are holding in Minecraft
Best Answer
To test if entities are at the same location, you can use the
r
argument to test for a radius of 0:You can use dy, dx and dz to test for an entity inside a cuboid relative to another entity. For example:
Would say players in this area (and 5 upwards):
To test for entities in the positive Z direction, you probably want a region that looks something like this:
You can make this bigger, although it will get laggier the larger the area to be checked is.
We need to check both negative and positive X from the entity (it is not at one of the corners of the region). To do this, we could use two separate testfor commands. One for +Z +X, and one for +Z -Z.
We can also use the coordinates in the execute command to have a different starting point, like this:
So we go -7 in the x direction, and then search a 17 by 5 by 10 region starting from there:
Applying this to your bat situation:
Should detect bats in the positive x direction. You'll need to create a command for each negative and positive in each axis. For example, for positive z: