Facing and moving towards a player at a certain distance

In one of my games I've been facing a problem to "detect" a player.
Scenario:
An enemy unit is patrolling an area, and if the player moves within ex. 100 pixels they will automatically be detected and the enemies will face and move towards them.

Any tips on how to get this working ?

Comments

  • mrpacogpmrpacogp Member Posts: 400
    Hmmm. Create one attrib, self positionx+100, when collide that with other actor then...do what you want
    Its that?
  • LyboTechLyboTech Member Posts: 135
    Doesen't really solve the problem, I want the player to be "detected" if it's in within a certain distance, not if it collides with the enemy.
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited November 2012

    @Lybotech

    Hi, the following is untested but should work out (hopefully... ;-) ):

    Make 4 real integers, PlayerX and PlayerY, Enemy1X and Enemy1Y

    ---in the Player Rules
    Constrain Attribute self.Player.Position.X to PlayerX
    Constrain Attribute self.Player.Position.Y to PlayerY

    ---in the Enemy Rules
    Constrain Attribute self.Enemy1.Position.X to Enemy1X
    Constrain Attribute self.Enemy1.Position.Y to Enemy1Y

    Rule:When All
    PlayerX > Enemy1X -101
    PlayerX < Enemy1X +101
    PlayerY > Enemy1Y -101
    PlayerY < Enemy1Y +101
    Change Attribute self.Rotation to vectorToAngle(scene.Background.Player.Position.X,scene.Background.Player.Position.Y)
    ---the following if wanted----
    Otherwise
    Change Attribute self.Rotation to 0

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • LyboTechLyboTech Member Posts: 135
    Awesome, thanks a lot. This is exactly what I needed! :)
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited November 2012

    Glad it helped. :-) I've just thought, I think if you make the rotation change attribute behaviour a constrain, it should adjust its angle when the player is moving within the area. Also, the x and y in the vectorToAngle expression ideally should be the constrained attributes. So that bit of the rule in the Enemy now is:

    Constrain Attribute self.Rotation to vectorToAngle(PlayerX,PlayerY)

    Fingers crossed that works OK.

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • LyboTechLyboTech Member Posts: 135
    Yeah, I'm making a sidescroller - so I have no use for that. But thanks, I'll keep in mind
    :)
Sign In or Register to comment.