Is it possible to detect from where you being attacked from?

Started by SparkleArts on

Topic category: Help with modding (Java Edition)

Last seen on 11:06, 29. Mar 2024
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Is it possible to detect from where you being attacked from?

Is it possible to detect from where you being attacked from?

Like the Damage Tilt Mod or the Attack Indicators where you see a red arrow from where you being attacked.

Last seen on 00:55, 28. Mar 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yes, and I just figured it…
Mon, 01/02/2023 - 17:03

Yes, and I just figured it out by trial and error. The correct way to do this would involve trigonometry: you would need to calculate the angle of a vector from the attacker to the target, and then display the correct indicator based on the size of the angle. (This is made even more difficult by the fact that Minecraft's degree system goes from 0-180 and to -180, presumably to determine the direction of turns and stuff.) 

Because I don't remember next to nothing about trigonometry, I just made a procedure that determines which of eight directions the player is looking at, (the cardinal directions and the diagonals), using their Yaw. (Which, again, switches to negative numbers once it goes past 180.) Then, depending on where they're looking, whether or not the x or z positions of the attacker are greater or less than the x and z positions of the target can tell me which direction to display the overlay. 

For the cardinal directions, there needs to be a margin of error; otherwise it will only do the side indicators. Have the procedure check the cardinal directions first, and also check that the opposite value falls within a certain range. If the player is facing directly north, for example; and the attacker is in front, (their z value is greater than the player), also check that the x value of the attacker is within 0.3 blocks of the target. Otherwise, it will only ever display the left and right indicators even if the attacker is directly in front. If you check for the front and back first, you can then do the Left and Right indicators based on the other position value.

...It's not an elegant way to do it, but it works pretty consistently. 

Last seen on 11:06, 29. Mar 2024
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can you show me a Procedure…
Sat, 01/07/2023 - 16:49

Can you show me a Procedure Image of that?

Last seen on 14:45, 10. Mar 2023
Joined Sep 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Have you already learned how…
Wed, 01/11/2023 - 13:31

Have you already learned how to do this? Can you show me please?