Make an NPC look your direction?

Get help using Construct 2

Post » Mon Dec 02, 2013 6:15 am

Okay, so I'm setting up an NPC that will look towards me as I move past.

I set up three animations, one with his head looking foward, one looking right, and one looking left.

I can easily make the person look left or right. The problem I run into is making them look forward again.

I actually did accomplish this, but I had to use trigger boxes. I set it so if the player is overlapping them, it sets the NPC's look animation. I have one for left, one for right, and if overlapping neither to look forward.

However, that method will add to the object count, and is not the optimal solution. That and I have to add these trigger boxes (sprites) in with every instance of an npc.

I wanted to do it with code. You know something like compare x. I had it working, but I couldn't get it to work with looking forward. I tried using "else." I tried using inverts. And so on, but nothing worked. It always either didn't look forward at all, or it ended up overwriting the look left command or something.

Also, I set it up so there is some play. I added +200 for the right, and -200 for the left so the NPC would look forward when you start to get close rather than when you are on top of him.

I'm using playerHitBox as my main thing in the events. So, if my hitbox is to the right or left, it makes the npc look.

Ex: (X < npc.x -200) or (x < npc.bboxleft -200) to look left

or (X > npc.x +200) or (X > npc.bboxright +200) to look right

Can anyone give me some guidance on what to do to make him look forward in between all this? I can't do it without interfering with one of the other directions. Thanks!
Posts: 340
Reputation: 22,721

Post » Mon Dec 02, 2013 8:04 am

Not sure what type of game it is, but if its top down, you can do it through an angle.


This gives your looking angle at the face.

With a couple "is between angles" you should be able to determine which way the head should look.
Who dares wins
Posts: 1,878
Reputation: 19,562

Post » Mon Dec 02, 2013 9:57 am

Would this work?

for each enemy
-System compare two values: abs(player.x-enemy.x)>200
--enemy.x>player.x - enemy look left
--enemy.x<player.x - enemy look right
-else - enemy look forward.

- = subevent
-- = subevent of subevent
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
Posts: 6,160
Reputation: 19,775

Post » Mon Dec 02, 2013 8:13 pm

Thanks, I got it sorted.

I forgot to mention it was a platformer. I guess I was tired.

Anyway, the real trick was the placement of the "else."

I had to put it in the middle of the two like this:

system - compare npc.x > player.x + 225 = look left
else = look forward
system - compare npc.x < player.x - 225 = look right

When I originally had else at the bottom rather than the middle, it would overwrite the look left command. I have no idea why. It seems like else would work regardless of placement, but I guess not.
Posts: 340
Reputation: 22,721

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests