Simple move towards player (4.5.9 platform modules)

baardbi

Well-known member
This is a modified version of a script @SciNEStist posted on Discord.

To make a monster always move towards the player in the platform modules you can make a new monster action. This will make sure that the monster always moves and faces towards the player.

1. Make a new file called MoveTowardsPlayer.asm

2. Put this code in the file and save it (I recommend putting this file in a AI folder):
;; Move towards player

TXA
STA temp
LDY player1_object

LDA Object_x_hi,y
CMP Object_x_hi,x
BCC +moveLeft
LDA #RIGHT
STA temp1
LDA #FACE_RIGHT
STA temp2
JMP +done
+moveLeft
LDA #LEFT
STA temp1
LDA #FACE_LEFT
STA temp2
+done
StartMoving temp, temp1
TXA
STA temp
ChangeFacingDirection temp, temp2

3. Assign this to a free Action Step in Script Settings. In this example I assign it to AI_Behavior5:
MoveTowardsPlayer.PNG

4. Rename the label for the Action to MoveTowardsPlayer. To do this go to Project Labels (in Project Settings) and rename AI_Behavior05.
MoveTowardsPlayerLabel.PNG

5. On the Action tab of your monster use this action for whatever action step you want. In this example I will use this on a monster with only one action step. That makes sure that the monster ALWAYS moves towards the player. If you have several action steps this step will only make the monster walk in the players direction when it reaches this action step. So even if you only have one action step you still need to make it repeat this action step (GoToFirst in this example).
MoveTowardsPlayerAction.PNG

That's it. Your monster will now always move in the direction of the player.
 

5kids2feed

Well-known member
Awesomeness! Here's the original code @SciNEStist sent me on discord for your monster to face the direction of the player without the monster moving too.

Code:
    TXA
    STA temp
    LDY player1_object
 
    LDA Object_x_hi,y
    CMP Object_x_hi,x
    BCC +moveLeft
        ChangeFacingDirection temp, #FACE_RIGHT
        JMP +done
    +moveLeft
        ChangeFacingDirection temp, #FACE_LEFT
    +done
 

baardbi

Well-known member
Here's an update from SciNEStist on Discord. He made an updated version of the script if you're using a scroller.

SciNEStist:
just as a note: if you are using a scroller, that script will act weird

TXA
STA temp

LDY player1_object
LDA Object_screen, y
CMP Object_screen, x
BEQ +checkxcoords
BCC +moveLeft
JMP +moveRight

+checkxcoords
LDA Object_x_hi,y
CMP Object_x_hi,x
BCC +moveLeft
+moveRight
ChangeFacingDirection temp, #FACE_RIGHT
JMP +done
+moveLeft
ChangeFacingDirection temp, #FACE_LEFT
+done

that checks if they are on the same screen first
 
any way to add up and down directions in this i got it to work in adventure module but im not sure how to add more movement directions
 

baardbi

Well-known member
Isn't there a "move towards player" in the adventure module? Let me check. I'll get back to you.
 

baardbi

Well-known member
any way to add up and down directions in this i got it to work in adventure module but im not sure how to add more movement directions
Here's an improved "move towards player" for the adventure module:

 
Here's an improved "move towards player" for the adventure module:

For whatever reasons I had issues getting that particular one to work that's why I was wondering if this one could be altered slightly because this one does work properly for me their code has a randomly switching facing directions for me and we couldn't figure out why
 

baardbi

Well-known member
For whatever reasons I had issues getting that particular one to work that's why I was wondering if this one could be altered slightly because this one does work properly for me their code has a randomly switching facing directions for me and we couldn't figure out why
Looks like they solved it in this thread:

 
Top Bottom