#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Enemys Constant moving to player

Posts: 64Member, BASIC

Hi Guys hope everyone is well.

Stuck again with a little problem if anyone could help, I have a scene about 5000 pixels wide and want to have enemy's spawn in from far sides of the screen and then move to the player and attack.

Problem is when the enemy's reach the players position they stop and dont continue to follow.

I have the usual real attributes Playerx & PlayerY constrained to player position X and Y.

Code so far -
In the enemys i have move to - PlayerX - PlayerY.

I also tried updating the PlayerX - Player Y every 0.5 seconds.

I also tried:

if Enemys position x is => - Move left
if Enemys position x is =< - Move Right

Which worked ok for a while but then decided to stop working and it also didn't seem to move actors that were way out on the edges of the scene. Any ideas??

• SwedenPosts: 717Member, PRO

Try accelerate toward playX,Y

• Posts: 9,914Member

Try wrapping that move to behavior in a loop.

• Posts: 1,483Member
edited May 2017

You can also do it this way

if self.position.x not = game.player.x
move to player.x player.y

• Posts: 64Member, BASIC

@The_Gamesalad_Guru Could you explain what you mean by wrapping the move to make a look please?

• Posts: 64Member, BASIC

*Loop

• Posts: 11,911Member, Sous Chef, PRO, Senior Sous-Chef
edited May 2017

He just means to place it inside a loop. So instead of:

Move To playerX, player Y

It would be:

Loop
Move To playerX, player Y

The reason that's needed is that the Move To behavior ends when it reaches that position. But if you have it in a loop, it's going to evaluate the end position each time based on playerX and playerY. Assuming they change values, it will move somewhere else.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Posts: 64Member, BASIC

Ahh got it, I had never even seen this type of behaviour yet very practical. One more thing if you could be so kind, When the enemy's reach the player i want them attacking the player it looks odd when they are directly on the players location i understand that i need to put Move to PlayerX + 30 to offset them slightly.

But as its a platformer and the player can move both ways how would i say

move to-
PlayerX-30 if left side and
PlayerX+30 if right side?

• Posts: 11,911Member, Sous Chef, PRO, Senior Sous-Chef

Perhaps do if enemyX is greater than playerX, stop at playerX+30; if enemy X is less than playerX, stop at playerX-30?

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User