How do I make a stalking enemy (on the x axis)?

0 favourites
  • 5 posts
From the Asset Store
Give Sound to the enemies that are part of your game! :)
  • Hi guys, I started playing around with Construct 2 the other night and it was really easy setting up basic things. My first questionmark happened when I tried to set up and enemy that would stalk my hero, but only on the x-axis. I tried setting up some variables on my objects but I couldn't find a way to change those in relation to another object.

    In pseudocode it would be something like:

    if stalker.x < hero.x then stalker.x++
    else if stalker.x > hero.x then stalker.x--
    else stalker stand still[/code:3rsl3zgx]
    
    Any help would be greatly appreciated.
  • Datasun

    Is this a side scroller where you want the stalker to start following the player if the player gets to the right of the stalker?

    Or more of a top down view and you want the stalker to try to stay aligned with the player in the X axis?

  • Hi, thanks for the reply! I was thinking of a (non-scrolling) platformer. Here's how I would represent it in words:

    If the enemy is to the left of the main character the enemy would move right.

    If the enemy is to the right of the main character the enemy would move left.

    If the character and enemy is at the same x position the enemy would stop.

    I just couldn't figure it out, so any help appreciated

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here you go. Any time the stalker isn't in line with the player's x value he will move to the player's x value. If you adjust the 0.02 you will adjust the speed at which the stalker aligns itself with the player.

  • Hey that's so nice of you, thank you very much, I relly appreciate it! It works perfectly. Now back to tinkering with my game. I guess I need to read the documentation and do some tutorials before thinking I can handle CS2 just like that.

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