[Solved] Enemy Frame Based Off Player Position

0 favourites
  • 8 posts
From the Asset Store
Selection frame like in RTS games, works both on mobile and desktop devices.
  • I need help with code. Basically, I want to be able to walk my player around a NPC and have the NPC's frame switch to match where my player is walking.

    For Example: Player moves to the Right of NPC, NPC's frame switches to the picture of the NPC facing right.

    I have pictures of the NPC facing in the 8 directions. N, NE, E, SE, S, SW, W, NW. I have messed around with angles, and the clockwise code but I just cannot get it to work. I thought I would ask the community as I am sure someone knows how to get this to work.

    Thanks in advance!

  • There are many ways to go about it but... for the most basic way:

    What I would do is get the angle from the npc to the player.

    You can then set the npc frame to round(angle/45). Set frame 0 to be facing east, frame 1 to be facing south east and so on.

    Does this make any sense?

  • There are many ways to go about it but... for the most basic way:

    What I would do is get the angle from the npc to the player.

    You can then set the npc frame to round(angle/45). Set frame 0 to be facing east, frame 1 to be facing south east and so on.

    Does this make any sense?

    Quick code insight, range is a circle sprite pinned to my player object. I use it to detect range as line of sight is useless ^_^

    Here is what I have done http://i.imgur.com/q9AVqRr.png and honestly it is the only way I can think of to actually get this to work. I'm useless when it comes to angles honestly. It works until I added in the part about playing animations, it will not play the animations in the section I highlighted in red... Also if I add in another enemy object only one of them will update. I will need it to update with multiple...

    Can anyone offer insight please? Or perhaps tell me what I am doing wrong.. Any help is greatly appreciated.

    Thanks in advance!

  • > There are many ways to go about it but... for the most basic way:

    >

    > What I would do is get the angle from the npc to the player.

    >

    > You can then set the npc frame to round(angle/45). Set frame 0 to be facing east, frame 1 to be facing south east and so on.

    >

    > Does this make any sense?

    >

    Quick code insight, range is a circle sprite pinned to my player object. I use it to detect range as line of sight is useless ^_^

    Here is what I have done http://i.imgur.com/q9AVqRr.png and honestly it is the only way I can think of to actually get this to work. I'm useless when it comes to angles honestly. It works until I added in the part about playing animations, it will not play the animations in the section I highlighted in red... Also if I add in another enemy object only one of them will update. I will need it to update with multiple...

    Can anyone offer insight please? Or perhaps tell me what I am doing wrong.. Any help is greatly appreciated.

    Thanks in advance!

    Ruskul gave you a great answer..

    set a variable on your enemy to round(angle(enemy.x,enemy.y,player.x,player.y)/45)

    if this variable is 0 or 8 set animation to east

    1 = southeast

    2 = south

    and so on..

    instead of your detection sprite you could check the distance between your enemy and player with distance(enemy.x,enemy.y,player.x,player.y)

  • >

    > > There are many ways to go about it but... for the most basic way:

    > >

    > > What I would do is get the angle from the npc to the player.

    > >

    > > You can then set the npc frame to round(angle/45). Set frame 0 to be facing east, frame 1 to be facing south east and so on.

    > >

    > > Does this make any sense?

    > >

    >

    > Quick code insight, range is a circle sprite pinned to my player object. I use it to detect range as line of sight is useless ^_^

    >

    > Here is what I have done http://i.imgur.com/q9AVqRr.png and honestly it is the only way I can think of to actually get this to work. I'm useless when it comes to angles honestly. It works until I added in the part about playing animations, it will not play the animations in the section I highlighted in red... Also if I add in another enemy object only one of them will update. I will need it to update with multiple...

    >

    > Can anyone offer insight please? Or perhaps tell me what I am doing wrong.. Any help is greatly appreciated.

    >

    > Thanks in advance!

    >

    Ruskul gave you a great answer..

    set a variable on your enemy to round(angle(enemy.x,enemy.y,player.x,player.y)/45)

    if this variable is 0 or 8 set animation to east

    1 = southeast

    2 = south

    and so on..

    instead of your detection sprite you could check the distance between your enemy and player with distance(enemy.x,enemy.y,player.x,player.y)

    I didn't know it was an answer to my problem. But I have a couple questions, again I am new sorry. I set a variable to round(angle(enemy.x,enemy.y,player.x,player.y)/45) but how exactly do I set the rest up? Do I use:

    Each Tick:

    use distance(enemy.x,enemy.y,player.x,player.y) to determine distance

    If within distance use round(angle(enemy.x,enemy.y,player.x,player.y)/45) to get the angle

    then a big if statement with all the individual possible outcomes?

    I have also run into the same problem as before, NE, N, NW do not work

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, I figured out the issue with it not working past 4, instead of up to 8 you need to go 0-4 then -1 through -4. However, can you still explain to me the most efficient way at layer these events? Be is per tick, or... How?

    Thank you by the way guys for the help so far

  • Well, I figured out the issue with it not working past 4, instead of up to 8 you need to go 0-4 then -1 through -4. However, can you still explain to me the most efficient way at layer these events? Be is per tick, or... How?

    Thank you by the way guys for the help so far

    Sorry for that, I wasn't aware the angle(x1,y1,x2,y2) gave negative values..

    If you have more than 1 enemy, I guess this would do:

    for each enemy

    distance(enemy.x,enemy.y,player.x,player.y) < 100

    -- instance variable = 0

    set animation ...

    --instance variable = 1

    set animation ...

    where -- are subevents..

  • > Well, I figured out the issue with it not working past 4, instead of up to 8 you need to go 0-4 then -1 through -4. However, can you still explain to me the most efficient way at layer these events? Be is per tick, or... How?

    >

    > Thank you by the way guys for the help so far

    >

    Sorry for that, I wasn't aware the angle(x1,y1,x2,y2) gave negative values..

    If you have more than 1 enemy, I guess this would do:

    for each enemy

    distance(enemy.x,enemy.y,player.x,player.y) < 100

    -- instance variable = 0

    > set animation ...

    --instance variable = 1

    > set animation ...

    where -- are subevents..

    It all works! Thank you so much. Unless you have a better idea to handle sprites facing the players object, that will be all!

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