How do I make a "tail-bite" mechanic?

I want to create a mechanic where if an unit gets behind another unit and when it’s behind, the other unit loses a bit health and delay for 1 second

1 Like

1 you will need to use unit touch another unit or entity trigger
2 if last unit type touched == triggering unit
set attributes of entity hp of last unit type touched == attributes of entity hp of last unit type touched - 1
3 there is no concept of behind or front in collision, you will need to make something more complex

1 Like

I thought I’ve already answered this before.

You can compare the angle between two units to the facing angle of one of them to check if its head/tail is facing the other unit. Put this in a script triggered when two units touch to detect when a unit bites another unit’s tail.

For the 1 second delay, you can make an attribute on the unit with min 0, max 1, and regenerate speed 0.25, so it will take 1 second to reach from empty to full. You can set it to empty when the unit’s tail is bitten and not let their tail be bitten again until the attribute is full again.

1 Like

Hmm, this doesn’t really work? I copied it to my game, but it doesn’t work… Could you please create a game to show me so I can copy it to my game?

1 Like

Alright, here https://www.modd.io/play/ktEBOAubm (sandbox link: https://www.modd.io/sandbox/game/ktEBOAubm)

1 Like

Hm, doesn’t seem to work with the units without collision, does it not work or am I doing something wrong?

1 Like

Units without collision will not trigger ‘when a unit starts touching another unit’. If you want the bodies to pass through each other but still trigger the script, you can enable sensor on the body type.

Okay, it kind of works now, but you have to go far from the back and you can turn to avoid it. Can we change it so that even if we’re already touching the unit, we can rotate and the bite is triggered?

1 Like

Yes, but that would involve constantly checking for units that are biting another unit’s tail. Here is an entity script that does that. It uses a for all entities loop to check for units in front of ‘this entity’.

Keep in mind that running loops for every entity this often may cause lag in your game.

1 Like