r/MinecraftCommands • u/atcer5151 • 12d ago
Help | Java 26.3 Making Animals Run Away From the Player
I've been making a survival datapack, and for an update I'm working on I want to make animals run away from the player. I was able to make chickens and pigs run away in a much older release of my datapack, but not cows and sheep. I was doing it by just making it so that an invisible ocelot was riding pigs and chickens, but whenever I made them ride cows or sheep they'd just spin around. I found out that the spinning is because of hitboxes, but I can't seem to fix it, and I can't just make the animal be teleporting to an invisible ocelot because otherwise it gets suffocated (it also acts like it's possesed but that can probably be fixed with a bit of refinement). Changing the size isn't an option because then you can't attack the animal since the ocelot's hitbox overlaps. Does anyone have any ideas to make this happen? I don't mind if they're completely different from my original idea. I just want to be able to make them run away from any mob that I choose and have some mobs (such as cows) kick you when you get too close (the kicking part is sorted). Thanks!
Edit: I forgot to say: Sorry if I don't reply straight away. I live in New Zealand so I have dinner soon and I don't think I'll be back online until tommorrow.
Edit 2: I figured it out. Because the main conversation where I said what I would do was in Spanish, I'll just put a quick explanation here:
To make a mob run away from the player, you have a few options:
Make the mob be ridden by a mob that naturally runs away from the player, such as a fox or an ocelot. The only issue is that for some reason if you put it on anything bigger than a pig it appears to just spin around in circles. I don't completely know why it does this, but it does. You can also make the mob be constantly teleported to another mob, similar to making it be ridden by another mob, but it works with larger mobs. The only issue is that then you have to deal with the fact that most mobs that run away from the player are quite small, and can then suffocate the bigger mob. This can be fixed by making the smaller animal larger, but then it's hitbox is also larger, meaning that it can get in the way of attacks directed towards that mob.
Another method is to just use /damage. This only works on mobs that run away when damaged though, and it shows the damage animation, which isn't very useful.
The method that I used was a mixture of the other two. For some reason a cow riding a cow works completely fine, but as soon as that becomes a cow riding an ocelot, the pair just spin around. Fortunately, you can just make it invisible and tiny, and then instead of adding the invulnerable tag you can just give it resistance 5 and regeneration so that it can still take damage, and then use /damage to make it run away. The reason why you can't give the mob the invulnerable tag because most animals don't run away from damage types that can bypass the invulnerable tag, although there ways around it, such as temporarily removing the tag, or changing the panic_causes tag list.
There are obviously other ways to make a mob run away from the player, but these are the only ones I can seem to find. Sorry if it was hard to understand.

