Set Animation According to Angle

0 favourites
  • 7 posts
From the Asset Store
Ninja char for your game! Make your own Shinobi game with this art.
  • Hey all,

    I am trying to make it so that when an object is moving towards a location, it will play the corresponding animation. For example, if their angle of movement is between 315 and 45, it plays the 'Right' animation.

    To calculate the angle, I have a family instance variable called AngleTracker which is set to 'angle(minions.x, minions.y, sprite9.x, sprite9.y)' as long as an enemy has instance variable 'Selected' to 1.

    I can get it to all work fine for right, down and left, but for some reason up (>225, <315) just won't work. They will still move in the correct way, but won't change their animation, instead always reverting to the 'Right' animation (>315 OR <45).

    I am guessing the issue has something to do with the fact that 'Right' uses an OR block, but I don't know how to get around that.

    Here's a link to the capx if someone could have a look for me, but be aware it uses 'moveto' and 'pushoutsolid' behaviours:

    I also tried to do it by pinning a pointer to each instance and point them towards the target and having each instance play the animation in accordance with their pointer's angle, but instead they would all reference only a single pointer's angle. If you have any ideas on that method it would also help!

    And just quickly, if anyone knows which path finding plugin would work best for this type of game it would be nice, I have only had a quick look at them so far.

  • You don't really need any variables or anything.

    There is a condition called 'Is between angles', you could use that. Then just give it an action such as 'change animation'

    And if you want it to detect angles when moving only then add another condition to the first one where it detects 'if moving' )Part of the 8-direction movement behaviour, but you could just tinker with it to not accept controls at all)

  • Thanks, but that won't work as the objects don't change angles themselves. I want the object to always remain at angle 0, but to calculate the angle they would have if facing another object/location. This is because they are side-on sprites so if they rotated towards any position it would look crazy ^^

    When they use 'moveto', they do not change angles. They just move in that direction at the current angle.

    I have managed a solution, where it manually checks the instance of the minion and that of the pointer, then sets the animation. It is very doable actually, but nowhere near as clean as I'd like, doubles the number of sprites on screen plus I'm interested to see why what I currently have doesn't work.

    EDIT: Actually it seems my work-around doesn't work, as they get stuck on frame 1 when travelling in any direction except right for some reason.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • First off what's the 'angletracker' actually doing? What's sprite9?

    there might be a better way of finding the angle of movement.

    (I'll just use the player/enemy object name: 'Body' cos it's what I'm used to)

    for example with platform behaviour you can just use:


    and for a physics object you can use:


    I haven't downloaded your cap because of the additional plugins, but also, you could try using 'anglediff' inside compare two values.. instead of 'is between values'. I find this generally works out any kinks with comparing angles

    for example:

    +For each Body

       +system, compare two values: anglediff(Body.angletracker,0) is less than 45

          ->set animation... etc

       +system, compare two values: anglediff(Body.angletracker,180) is less than 45

          ->set animation... etc

    as you've probably guessed, this checks to see if the movement angle is within 45 degrees of 0, or if it's within 45 degrees of angle 180

    -180 is Left, -90 is Up, 0 is Right, +90 is Down, and +180 is left again.

    If you want to use 8 directions, then just use the angles inbetween and 'is within 22.5 degrees of x' instead

  • I haven't looked at the capx either. But I'd recommend a nested IF. It will use a few more resources, but could resolve your problem.

    Also, always specify the between. The reason for this is that when looking at angles, -1 = 359, -60 = 300, 2 = 362, etc...

    IF (some condition)

    • code

    ELSE IF (next condition)


       ELSE IF (next condition)

       - code

    By using this you will also know which condition is met, and can debug better. Because in all likelihood, two or more of your conditions are true.

    You can also do some smart algorithm when checking your angles, something like:

    IF angle < 0 THEN SET angle = angle + 360

    Then after ensuring that they are always between 0 and 360 apply your conditions.

  • keepee & superkew

    Thanks for your help guys! Keepee's method works just fine, but out of curiosity, how is that different from what I was doing? I mean it looks like your method and mine should achieve the same results.

    And thanks for debugging tip Superkew, I can see that being useful :)

  • It might have been because of how you returned the movement angle..

    doing it the ways I mentioned above means that up is returned as -90, (not 270).. it was probably the same for the way you originally set it up.

    anglediff works regardless of whether you put in -90 or 270 or even something like -1000..

    I think it's the same for other angle related expressions/actions like anglelerp and 'is clockwise from' for example.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)