How To Make A Enemy Follow A Character

Discussion in 'How Can I...?' started by brecon1313, Aug 7, 2022.

  1. brecon1313

    brecon1313 Boxer

    Joined:
    Jul 22, 2022
    Messages:
    9
    Likes Received:
    0
    I'm very new to game design and coding in general so I wanted to see if anyone could help me with this. I am trying to make an enemy follow my character in a 2d game in Buildbox 3. I follow a Buildbox thread from a couple years ago and it got me in the write direction but it not exactly what I'm looking for. I attached an image of the current code. It is making the enemy follow the character, but it swings around it and goes through wall instead of just statically following the character. Any advice is greatly appreciated! :)

    The thread I followed is "How Can I Make The Enemy Gravitate Towards The Character (actor)"
     

    Attached Files:

  2. Ivan Perfetti

    Ivan Perfetti Avid Boxer

    Joined:
    Sep 9, 2018
    Messages:
    203
    Likes Received:
    181
    Hi there and welcome!
    I'm pretty sure you can find many discussions about this in the forum (try looking for "magnet" or "follow").
    I can see the logic in your node and given the fact you're new to coding this is not bad; the problem with this approach is that it slowly adjusts the entity position so it's not ideal, there are better solutions.
    A quick suggestion: when you use variables (like LR and UD) you should make sure to declare them first and when you end a line you should add a semicolon at the end of it.

    To declare those two variables just write this on top of your script:
    Code:
    let LR;
    let UD;
    This make sure you avoid issues caused by undeclared variables.

    About a better solution to the "follow" problem a very good and simple one is to calculate the direction vector between the following entity and the followed one and use that value as an input for a "Move in direction" node, that's an excellent node you can find prebuilt into Buildbox; this node usually takes a joystick input and is able to control the movement of the entity accordingly.
    It's nothing too difficult and you can find a demo bbdoc in the attachment showing it at work in both 2D and 3D worlds.
    The custom node doing the magic is named "Follow Entity" and you'll find it inside the "Follower" entities in the BBDOC.


    The node has a few attributes so that you can easily configure it:
    Target: select a target asset, and the follower entity will look for it and follow it.
    Speed: the following speed.
    2D: tick this if you're using the node in a 2D world


    I'm going to post this on itch.io as a free node with a demo bbdoc so that anyone can easily retrieve and use it.
    Let me know if you need any direction.
     

    Attached Files:

  3. brecon1313

    brecon1313 Boxer

    Joined:
    Jul 22, 2022
    Messages:
    9
    Likes Received:
    0
    Hey Ivan! Thanks so much for the reply. I tried to open the example you gave but it had an error message while loading. It said "This BBDOC cannot be loaded, it appears to have been saved with a newer version of Buildbox." I believe I have the newest version of both Buildbox 3 and Buildbox Classic. Do you know how to fix this? Thank so much again for the help!
     
  4. Ivan Perfetti

    Ivan Perfetti Avid Boxer

    Joined:
    Sep 9, 2018
    Messages:
    203
    Likes Received:
    181
    Hey there, that's been saved with BB 3.4.9, the latest one!
     
  5. brecon1313

    brecon1313 Boxer

    Joined:
    Jul 22, 2022
    Messages:
    9
    Likes Received:
    0
    Perfect! It works amazingly! Thank you so much. Is there a way to make the followers move slower towards the character?
     
  6. Ivan Perfetti

    Ivan Perfetti Avid Boxer

    Joined:
    Sep 9, 2018
    Messages:
    203
    Likes Received:
    181
    You're welcome, to make the followers move slower just change the value of the speed attribute.
     
  7. brecon1313

    brecon1313 Boxer

    Joined:
    Jul 22, 2022
    Messages:
    9
    Likes Received:
    0
    Hey Ivan! This has helped me so much. I've added my own enemy animation to it, but do you know how to make the animation face a certain way while moving certain direction so it will look like the enemy is facing the character while moving towards it? Thanks again!
     
  8. Ivan Perfetti

    Ivan Perfetti Avid Boxer

    Joined:
    Sep 9, 2018
    Messages:
    203
    Likes Received:
    181
    Hey there, the "Move in direction" node supports that, it has an attribute to rotate to face forward but I'm afraid that would only work well in a 3D setup so you might need a different solution in a 2D world; an alternative solution would be to use a 3D World and set the camera as "Orthogonal".
     

Share This Page