4.5.9 Move Towards Player AI: Improved Version

Bucket Mouse

Active member
There exists, in the Adventure Module, a "Move Towards Player" AI that causes monster sprites to move toward your own sprite when you enter a room. However, this script contains no information for the monster to change its facing direction, so unless you designed your monster to make sense from all angles, it'll often be looking in the other direction while chasing you.

Until now, no one knew what to do about that. I asked Joe for hints on how to solve this, and he gave me a few...but the actual solution turned out to be sort of different from what he proposed. Anyway, this works, and if you replace the current Move Towards Player AI script with this one, the monsters will always stare you down as they chase you, as they should.

Code:
;;; aim towards player.
    TYA
    PHA
    TXA
    PHA
    STA tempx
   
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
       
        LDX tempx
       
    MoveTowardsPoint tempA, tempC, tempB, tempD
    LDA Object_direction,x
    AND #%00000111
    ORA #%00001000
    STA Object_direction,x
   
LDA tempA ; horizontal
CMP tempB ; vertical
BCS verticalCompare

horizontalCompare:
LDA tempA
BPL movingRight

movingLeft:
LDA #6
STA tempy
JMP +

movingRight:
LDA #2
STA tempy
JMP +

verticalCompare:
LDA tempB
BMI movingUp

movingDown:
LDA #4
STA tempy
JMP +

movingUp:
LDA #0
STA tempy
JMP +

+
    TXA
    STA tempA
    TAX
ChangeFacingDirection tempA, tempy
   
    PLA
    TAX
    PLA
    TAY
 

Kanbei85

Member
Both this fixed script and the original have the problem that they somehow override all other physics-based commands, such that the monsters don't recoil or stop. Once it starts in motion, an action step command to "stop moving" has no effect. Recoil doesn't happen, so the health is instantly depleted in one hit.
 

IMBrendan

Member
NOTE: Kanbei85's issue was solved in this post - Buckmouse's Stop moving action was different than the one Kanbei85 was using.

I do have a question however - I would love to hijack this script to have enemies that run from the player at poximity. Do you guys think that's possible here?
 
There exists, in the Adventure Module, a "Move Towards Player" AI that causes monster sprites to move toward your own sprite when you enter a room. However, this script contains no information for the monster to change its facing direction, so unless you designed your monster to make sense from all angles, it'll often be looking in the other direction while chasing you.

Until now, no one knew what to do about that. I asked Joe for hints on how to solve this, and he gave me a few...but the actual solution turned out to be sort of different from what he proposed. Anyway, this works, and if you replace the current Move Towards Player AI script with this one, the monsters will always stare you down as they chase you, as they should.

Code:
;;; aim towards player.
    TYA
    PHA
    TXA
    PHA
    STA tempx
  
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
      
        LDX tempx
      
    MoveTowardsPoint tempA, tempC, tempB, tempD
    LDA Object_direction,x
    AND #%00000111
    ORA #%00001000
    STA Object_direction,x
  
LDA tempA ; horizontal
CMP tempB ; vertical
BCS verticalCompare

horizontalCompare:
LDA tempA
BPL movingRight

movingLeft:
LDA #6
STA tempy
JMP +

movingRight:
LDA #2
STA tempy
JMP +

verticalCompare:
LDA tempB
BMI movingUp

movingDown:
LDA #4
STA tempy
JMP +

movingUp:
LDA #0
STA tempy
JMP +

+
    TXA
    STA tempA
    TAX
ChangeFacingDirection tempA, tempy
  
    PLA
    TAX
    PLA
    TAY
so the code does work but I'm having a strange issue... randomly it seems the monster will flip what its doing and lock into a facing direction for a while resulting in the monster moonwalking lol. I've checked my animations and what animations are assigned to what direction. they all look good. any idea what it could be causing this?
 

Bucket Mouse

Active member
Never heard of that happening, and haven't run into the issue myself. It's possible for the sprite to display the wrong movement for a little bit while changing angles, but I haven't personally seen anything as bad as walking backwards.
 

Ekenz

New member
There exists, in the Adventure Module, a "Move Towards Player" AI that causes monster sprites to move toward your own sprite when you enter a room. However, this script contains no information for the monster to change its facing direction, so unless you designed your monster to make sense from all angles, it'll often be looking in the other direction while chasing you.

Until now, no one knew what to do about that. I asked Joe for hints on how to solve this, and he gave me a few...but the actual solution turned out to be sort of different from what he proposed. Anyway, this works, and if you replace the current Move Towards Player AI script with this one, the monsters will always stare you down as they chase you, as they should.

Code:
;;; aim towards player.
    TYA
    PHA
    TXA
    PHA
    STA tempx
 
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
     
        LDX tempx
     
    MoveTowardsPoint tempA, tempC, tempB, tempD
    LDA Object_direction,x
    AND #%00000111
    ORA #%00001000
    STA Object_direction,x
 
LDA tempA ; horizontal
CMP tempB ; vertical
BCS verticalCompare

horizontalCompare:
LDA tempA
BPL movingRight

movingLeft:
LDA #6
STA tempy
JMP +

movingRight:
LDA #2
STA tempy
JMP +

verticalCompare:
LDA tempB
BMI movingUp

movingDown:
LDA #4
STA tempy
JMP +

movingUp:
LDA #0
STA tempy
JMP +

+
    TXA
    STA tempA
    TAX
ChangeFacingDirection tempA, tempy
 
    PLA
    TAX
    PLA
    TAY
Is there a way to adjust the speed at which it targets the player? I tried increasing the speed variable but I dont think this is using the same variable for speed.
 

dale_coop

Moderator
Staff member
Is there a way to adjust the speed at which it targets the player? I tried increasing the speed variable but I dont think this is using the same variable for speed.
Check that topic:
 

tbizzle

Well-known member
so the code does work but I'm having a strange issue... randomly it seems the monster will flip what its doing and lock into a facing direction for a while resulting in the monster moonwalking lol. I've checked my animations and what animations are assigned to what direction. they all look good. any idea what it could be causing this?
Yeah, I can't get my monster to use the Up_Right or Down_Right animation. Looks kinda weird running around like that lol. Any fixes for this???
 

SHLIM

Active member
Yeah, I can't get my monster to use the Up_Right or Down_Right animation. Looks kinda weird running around like that lol. Any fixes for this???
the code doesnt check for diagnal facing positions, if you look it only checks UDLR. You can easily add the extra facing positions in by copying the code but changing the numbers that represent the direction. (ie LDA #1 ). but then youl also need to check both hori and vert positions together.
however, the animations do walk diagnally, so im not sure if the directions loaded in that script are really UDLR (0 2 4 6) or if a couple of those numbers are diagnal directions? ive never messed with directions using numbers, i usually use this format #%00000011
I guess you could mess with the numbers and see what happens.
 
Top Bottom