[Solved] Enemy Frame Based Off Player Position

Get help using Construct 2

Post » Fri Nov 13, 2015 12:54 am

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!
Last edited by JHunter on Tue Nov 17, 2015 4:16 am, edited 1 time in total.
B
13
S
7
G
1
Posts: 31
Reputation: 1,866

Post » Fri Nov 13, 2015 1:43 am

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?
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Mon Nov 16, 2015 11:40 pm

ruskul wrote: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!
B
13
S
7
G
1
Posts: 31
Reputation: 1,866

Post » Tue Nov 17, 2015 12:00 am

JHunter wrote:
ruskul wrote: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 told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Nov 17, 2015 12:53 am

LittleStain wrote:
JHunter wrote:
ruskul wrote: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
B
13
S
7
G
1
Posts: 31
Reputation: 1,866

Post » Tue Nov 17, 2015 1:02 am

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
B
13
S
7
G
1
Posts: 31
Reputation: 1,866

Post » Tue Nov 17, 2015 1:29 am

JHunter wrote: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..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Nov 17, 2015 3:43 am

LittleStain wrote:
JHunter wrote: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!
B
13
S
7
G
1
Posts: 31
Reputation: 1,866


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 50 guests