How Can I Make The Enemy Gravitate Towards The Character (actor)

Discussion in 'How Can I...?' started by teofil, Sep 18, 2019.

  1. teofil

    teofil Boxer

    Joined:
    Sep 6, 2019
    Messages:
    8
    Likes Received:
    0
    What I have is a character in the middle of the screen and enemies behind him. What I want to do is, make the enemies gravitate towards the character. Any idea how can I do this?
     

    Attached Files:

  2. Scriv

    Scriv Boxer

    Joined:
    Oct 28, 2018
    Messages:
    50
    Likes Received:
    22
    in Nite Fighter i did this with 2 variables per enemy.
    UD for up and down
    LR for left and right.
    then after conditioning the position of the main character in relation to the enemy you can use those variables to make the enemy move towards the character.

    in the update function of the enemy my code was something like this.... (writing from scratch probably have errors in it)

    let enemypos = this.entity().worldPosition();
    let character = this.scene().find('Actor')[0];
    let characterpos = character.worldPosition();

    // then to check the x axis i wrote something like this (do the same with UD for the y axis)//

    if (enemypos.x >= characterpos.x){
    LR -= 0.1
    }else{
    LR +=0.1
    }

    // then i add the LR variable to the current position of the enemy to make it move towards the character //

    this.entity().setPosition(enemypos.x+LR,enemypos.y+UD,enemypos.z);

    essentially you're constantly updating two variables to 'swing' towards the character.

    good luck
     
    Blacklist likes this.
  3. Steve K Roberts

    Steve K Roberts Boxer

    Joined:
    Mar 1, 2021
    Messages:
    5
    Likes Received:
    0

Share This Page