How do I change enemy animation depending on angle to player?

0 favourites
  • 6 posts
From the Asset Store
Be discreet and rescue your friends from behind enemy lines.
  • I have a top down game. I have a base sprite that is the hitbox of my enemy AI then a sprite above that. I do not want the hit box rotating because it will cause problems with player melee animations.

    I have used a 3rd sprite with turret behavior to determine angle every tick and depending on the angle/state is changes the animation. Is there a better way to do this? This will be a horde game so If I am having 100 enemies on the layout there will essentially be 300 plus objects in the scene. Will this start to cause a performance drop ?

    Is this fine or is there a better way ?

  • Are you using Turret behavior just to determine the angle? I'm guessing it won't be good for performance. You can use this expression:

    angle(self.x, self.y, player.x, player.y)

    This will probably be the most efficient way:

    On every tick: 
    
     Enemy set variable a to angle(self.x, self.y, player.x, player.y)
    
     Enemy set animation to (anglediff(a, 0)<45 ? "Right": anglediff(a, 90)<45 ? "Down": anglediff(a, 180)<45 ? "Left": "Up")
    

    "a" is an instance variable on the enemy sprite.

  • Are you using Turret behavior just to determine the angle? I'm guessing it won't be good for performance. You can use this expression:

    angle(self.x, self.y, player.x, player.y)

    This will probably be the most efficient way:

    > On every tick: 
    
    Enemy set variable a to angle(self.x, self.y, player.x, player.y)
    
    Enemy set animation to (anglediff(a, 0)<45 ? "Right": anglediff(a, 90)<45 ? "Down": anglediff(a, 180)<45 ? "Left": "Up")
    

    "a" is an instance variable on the enemy sprite.

    I am having trouble figuring out to compare the instance variable, like how do I write this out using events.Line 5 not line 3. Would you have a example of a event sheet using this way ?

  • I made a small mistake, it should be self.a in the last line.

    And of course put the correct animation names instead of "Left", "Right" etc.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I made a small mistake, it should be self.a in the last line.

    And of course put the correct animation names instead of "Left", "Right" etc.

    what do you mean self.a in last line?

    It should be anglediff(self.a,359)<45?"up") ? The equation wont let you end it like that it demands another ":" and so forth

  • Yes, self.a instead of just "a", but you need to copy/paste this entire expression:

    (anglediff(self.a, 0)<45 ? "Right": anglediff(self.a, 90)<45 ? "Down": anglediff(self.a, 180)<45 ? "Left": "Up")
    

    It's using ternary operators and is a short and efficient way to do this:

    If angle is near 0 degrees - use "Right" animation
    else If angle is near 90 degrees - use "Down" animation
    else If angle is near 180 degrees - use "Left" animation
    else use "Up" animation
    
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)