Pixelguy Posted October 8, 2017 Share Posted October 8, 2017 Hey there, I'm trying to get an AI to follow another sprite along the Y axis but as soon the exact Y location is almost reached the AI sprite gets jittery due to constant overshooting. What would be the correct way to implement such a behavior while using a fixed velocity? I've included a simple example below where the sprite should move to game.height/2 : The usage of the offset variable seems hacky and wrong. (Set 'offset = 10' to stop the jitter). And thanks in advance, it's been a few years since I last touched phaser Link to comment Share on other sites More sharing options...
samid737 Posted October 9, 2017 Share Posted October 9, 2017 You can use physics to let the AI/enemy follow the player. Use the difference in position between the AI and the player and set the velocity equal to that difference . You can use drag and mass properties to adjust acceleration towards player: For synced motion you could just set the exact same y position (no need for physics), velocity, acceleration as the player (commented lines in example). Another way is to use tweens to animate without physics, but you can use easing functions to customize the motion of the robot/AI: In general, you could also make one object follow another object using moveToObject: So there are different options to consider, but the simplest is just to say robot.y= player.y. Pixelguy 1 Link to comment Share on other sites More sharing options...
samme Posted October 9, 2017 Share Posted October 9, 2017 That's normal, I think you just need to pick a zone or a distance that's "close enough" and stop it there. Pixelguy 1 Link to comment Share on other sites More sharing options...
Pixelguy Posted October 9, 2017 Author Share Posted October 9, 2017 Thanks, thats what I ended up doing. Thank you for the examples samid737 For future reference here is the code I ended up with: updateEnemy: function() { if ( game.math.difference(this.enemy.y, this.ball.y) > game.globals.enemyMovementDeadzone ) { if ( this.ball.y < this.enemy.y ) { this.enemy.body.velocity.y = -game.globals.maxEnemySpeed; } else { this.enemy.body.velocity.y = game.globals.maxEnemySpeed; } } else { this.enemy.body.velocity.y = this.ball.body.velocity.y; this.enemy.body.maxVelocity.y = game.globals.maxEnemySpeed; } }, samid737 1 Link to comment Share on other sites More sharing options...
Recommended Posts