[TUTORIAL] Solving the Mismatch Between Geckolib Entity Attack Animation and Attack Timing in Mcreator

Started by C.Jiangqiu on

Topic category: Advanced modding

Joined May 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[TUTORIAL] Solving the Mismatch Between Geckolib Entity Attack Animation and Attack Timing in Mcreator
Sun, 02/18/2024 - 12:46 (edited)

(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

Edited by C.Jiangqiu on Sun, 02/18/2024 - 12:46
Joined Jun 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Legend
Mon, 03/11/2024 - 21:14

Legend

Joined May 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Created an account just to…
Sat, 05/25/2024 - 07:15

Created an account just to say you are such an amazing person. Literally saving people hours of work and debugging

Joined Jun 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how do i get the [sqrt((x2…
Tue, 07/02/2024 - 18:34

how do i get the [sqrt((x2-x1)2+(y2-y1)2+(z2-z1)2)] block

Joined Nov 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
replying to VoldeCoco5464: …
Sat, 08/03/2024 - 07:40

replying to VoldeCoco5464:

  • first, get the "square root" block (in the math tab, probably looks like "root" for now but click and change that)
  • then add the "X position of source entity" (left) and "X position of target entity"(right)  in a - symbol block and connect it to the square root
  • get the one with the ^ symbol (change from the + symbol block) and put your square root in the left, and 2 in the right
  • then copy and paste it so you have three of them, and change "X position" to "Y position" and "Z position"
  • connect them all in two + symbol blocks
  • and put that in <= 2 block

that should be all :)