Change animation on collision

Hi all! So sorry I’ve been asking so many questions i’m new to playcanvas and i really wanna learn hehe :sweat_smile::sweat_smile: and for my finals I have to add an animation on collision, meaning when the enemy collides with the player, it will play the “attack” animation. I already have it in the state graph but it won’t play for some reason and I can’t seem to figure out the code.

The running animation works when in proximity with the player, but I’m trying to make it switch to “attack” when it collides with the player, and it’s not workinf :frowning: thank you all so much for helping me so far! <3

I would just use the distance instead of collision as you already use this for the other states as well.

Make sure the conditions of every transition is correct and use a different distance for every state.

oh okay! so no changes in the script, right? Just changing the Distance<0.1(or other numbers) in the animstate graph? Because now the running animation works but the “'attack” animation makes the enemy float in the air :frowning:

Yes, something like that indeed.

I guess this has nothing to do with the transition of your animations. The reason is probably because you use lookAt() in combination with the position of the player entity. The origin of the player entity is not on the ground.


ahh okay, i had to adjust the Y origin because it didn’t hit the floor, does that mean it always has to be 0? because when it does, the enemy kind of floats over the floor :// thank you!

No, you can also adjust your script and only look at the x-axis and z-axis of the player position. For the y-axis you can use the position entity itself. Below a code example.

var playerPosition = playerEntity.getPosition();
var enemyPosition = this.entity.getPosition();
this.entity.lookAt(new pc.Vec3(playerPosition.x, enemyPosition.y, playerPosition.z));