Topic category: Advanced modding
(I used a translator to write this tutorial, so I'm sorry if there are any errors)
When using geckolib to create entity attack animations for yourself, beginners in Mcreator often find that for some longer attack animations, the attack animation has not finished playing, but the entity has already completed the attack and caused damage. This is because when an entity attacks another entity, it will call the doHurtTarget method, which will immediately calculate the damage. For this, we choose a clever way to make our entity correctly cause damage while playing the attack animation.
First, I assume your Minecraft version is 1.20.1, the API is Forge, and you have installed the following plugins in your Mcreator: geckolib, attributes. At the same time, I also assume that you have completed the initial creation of the animated entity.
Next, go to your animation page. Like this:https://postimg.cc/Yh5ys8Sv
Deselect the attack animation of your entity and then save. (In the code page, it should be in the following state:https://postimg.cc/rK0Pj7q6). Next, create a procedure, such as: “(Your entity name) Attack”, then it’s time for the fun and easy task of building blocks:1 3en — Postimages (postimg.cc)
Let’s explain the idea here: First, when our animated entity attacks, we first cancel this event, at this time the entity will not cause attack damage, then play our attack animation defined in Blockbench, next, we wait for a period of time (according to your idea, the time here is also different, for example, I decide to cause damage when playing halfway, the specific value you can decide according to your entity’s attack method and performance in the game). After that, we judge the distance between the two entities and whether the attacking entity is facing the attacked entity, using the simple calculation of the straight-line distance in space [sqrt((x2-x1)2+(y2-y1)2+(z2-z1)2)] can complete the implementation of your entity’s attack distance, the 2 in "<=2" can be replaced with the attack distance you want. Then enter in the custom code :
!(entity instanceof LivingEntity && ((LivingEntity) entity).isUsingItem() && ((LivingEntity) entity).getUseItem().getItem().equals(Items.SHIELD))
This code may become invalid with the update of the version. This custom code makes the attacked entity if not holding a shield to block, our later part of causing damage will take effect, but if holding a shield, then this damage will not take effect, at the same time, it will simulate the effect of shield blocking damage.
Okay, if you have a better way, please let me know, and you can find the code here as an example.https://github.com/CJiangqiu/CJiangqiu-mcreator-tutorial-demonstration-project
Legend
Created an account just to say you are such an amazing person. Literally saving people hours of work and debugging
how do i get the [sqrt((x2-x1)2+(y2-y1)2+(z2-z1)2)] block
replying to VoldeCoco5464:
that should be all :)