Make an NPC look your direction?

  • 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!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Not sure what type of game it is, but if its top down, you can do it through an angle.

    angle(player.X,player.Y,face.X,face.Y)

    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.

  • 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

  • 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.

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