Phaser 3 auto aim

by Trobing

HTML

<script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser.js"></script>

JavaScript

var config = {
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        backgroundColor: '#cccccc',
        physics: {
            default: 'arcade',
            arcade: {
                gravity: {y: 300},
                debug: false
            }
        },
        scene: {
            preload: preload,
            create: create,
            update: update
        }
    };

    var game = new Phaser.Game(config);

    var platforms,
        player,
        cursors,
        bomb;

    function preload() {
        this.load.setBaseURL('https://labs.phaser.io');
        this.load.image('bomb', 'assets/sprites/blue_ball.png')
        this.load.image('ground', 'assets/sprites/platform.png')
        this.load.spritesheet('dude', 'assets/sprites/dude.png', {frameWidth: 32, frameHeight: 48})
    }

    function create() {
        platforms = this.physics.add.staticGroup();
        platforms.create(400, 568, 'ground').setScale(2).refreshBody();
        
        player = this.physics.add.sprite(400, 450, 'dude', 4);
        player.setCollideWorldBounds(true);
        
        bomb = this.physics.add.sprite(100, 0, 'bomb')
        bomb.setBounce(1)
        bomb.setCollideWorldBounds(true)
        bomb.allowGravity = false
        bomb.setVelocity(player.x - bomb.x, player.y - bomb.y)
        
        cursors = this.input.keyboard.createCursorKeys();

        this.physics.add.collider(player, platforms);
        this.physics.add.collider(bomb, platforms);
        this.physics.add.overlap(player, bomb, hitBomb, null, this);
    }


    function update() {
        if (cursors.left.isDown) {
            player.setVelocityX(-200)
        } else if (cursors.right.isDown) {
            player.setVelocityX(200)
        } else {
            player.setVelocityX(0)
        }

        if (cursors.up.isDown && player.body.touching.down) {
            player.setVelocityY(-300)
        }
    }

    function hitBomb(player, bomb) {
        this.physics.pause()
      ...