AI Combat Interaction

So, I recently made a dog for my survival game. I’m sure the terms “dog” and “survival” gave out a clue, didn’t it? If you didn’t catch the clue, I’m saying that if a dog gets too close to you, the dog bites on your arm. Then you’re forced to wrestle or fight the dog off.

Something like this:

How do I accomplish something like this?

Make an animation of the dog biting your arm. Use a near sensor to play that animation.

I was considering that, but there’s one concern: Inconsistent/unaligned rotation.

The player/AI humanoid might not be facing the dog and vise versa.

Then you need to use a couple frame to align the player with the dog, something like

direction = player.worldPosition - dog.worldPosition
direction.z = 0
player.alignAxisToVect(direction, 2, percentage)


The idea is to run this piece of code with a percentage of say, 0.2, in order to make your player rotate towards the dog.
(you need to run it on multiple frames to actually rotate smoothly the player)

I know alignAxisToVect can do weird things, but you could also play with worldOrientation or whatever.

+1 to wknight02’s solution, for a visual example, check out alien isolation, specifically when the player is caught by an android. The functionality there is exactly what you’re looking for.

Thanks, this has helped me very much!

I’ll keep you guys updated, and even post a blend on here.

So I have one question in mind. How do I use the worldOrientation method?

Use this webpage:

You can find everything in it, everything I could tell you about a function comes from there in reality.


Aye, thanks. I, unfortunately, found it like minutes ago. I also constantly use that webpage to hep me.

Another interesting idea is to use copy location constraints to drive your dog’s head ik animation and then use python to lock the ik target on to the position of the player’s arm, and since your dog’s ik rig will react dynamically in real time you probably could get away with fewer keyframes or none at all for an attack animation

this approach simplifies the whole procedure, and exactly why procedural animation saves time

I know your approach, and I might go for it. The problem is I don’t know how to do any of that. So here’s a specific series of questions:

So how would I drive the dog’s head IK animation with copy location constraints?

How would I make Python lock onto the IK target to the player’s arm?

Yes but if the dog comes biting from behind, and you have an animation where it gets your arm in his jaws, you might want to make the player rotate a bit before playing any IK drive animation ? Never done it so idk…

Well if the dog bites from behind, the player could just turn around instantly, and then so forth.


I haven’t tested any of this either but in theory snapping the dog onto the position of the arm will work from behind or front


basically the ik target bone is the one moving the entire ik chain during the game, by combining the copy location constraints, the ik target can now copy another game objects(lets call it snapper) position, this is how foot planting is done in bge

the python script to control snapper’s position can either be triggered by raycast or collision, depends

So how could I make the dog’s head snap to the player’s arm? I got the snapper segment down, I’m just curious to know about the how they’re going to snap.

I assume that this requires an empty in the dog’s mouth and on the player’s arm?

I’ll test this out when I can.

you can use empty or cube hit box for them, the simple script below only requires these objects position and leave the rest to the dog’s ik animation

arm = # insert your object
jaw = # insert your object

dist = (arm.worldPosition - jaw.worldPosition).length

if dist < 1:
    jaw.worldPosition = arm.worldPosition

AFAIK, dogs don’t care if you are facing the right direction when they attack you…to me…it would be annoying to lose control of my character due to this mechanic…from a players perspective…

…in other words.

You may be making this more difficult than it needs to be…ofc, only you can decide and I will leave you with that. :slight_smile:

What about distance? My player and my dog are being aligned to their origins.

the distance is just a trigger, the jaw snapping to the arm is the only thing that matters

Aye, okay. I did actually make them snap each other at the origins so no errors would occur. I was considering on constraining the dog a certain distance.

So, I have another question: How do I make the dog stagger?

Since you have the ability to fend dogs off after you punch the nose several times.