How do i make an entity move closer to the player

Why you don’t use the bit of script I sent you earlier? You just have to make sure it fits your setup.

var distance = this.entity.getPosition().distance(player.getPosition());

if (distance > 1 && distance < 10) {
    // look at the player (probably your model need to be rotated 180 degrees)
    this.entity.lookAt(player.getPosition());
    // move forward (if the rigidbody is kinematic)
    this.entity.translateLocal(0,0,-0.1);
}

[quote=“Albertos, post:15, topic:19345”]

var distance = this.entity.getPosition().distance(player.getPosition());

if (distance > 1 && distance < 10) {
    // look at the player (probably your model need to be rotated 180 degrees)
    this.entity.lookAt(player.getPosition());
    // move forward (if the rigidbody is kinematic)
    this.entity.translateLocal(0,0,-0.1);
}

does it know what Player is referring to?

not unless you assign some other entity to it. You can find entity by name in the scene perhaps and assign it to the player variable.

1 Like

how will i do that

search is your friend: Find object in current scene

1 Like

var player=this.app.root.findByName(name of the player entity);

This?

var distance = this.entity.getPosition().distance(player.getPosition());

if (distance > 1 && distance < 10) {
    // look at the player (probably your model need to be rotated 180 degrees)
   var player=this.app.root.findByName(name of the player entity);
 this.entity.lookAt(player.getPosition());
    // move forward (if the rigidbody is kinematic)
    this.entity.translateLocal(0,0,-0.1);

it says, Getposition is undefined

That is because you use the variable player already in your first line, so you have to define the variable before the first line, like how I did it below. In addition, you have to change the name of the player enitity in the findByName function as you have named it in your scene.

var player = this.app.root.findByName('name of the player entity');
var distance = this.entity.getPosition().distance(player.getPosition());

if (distance > 1 && distance < 10) {
    // look at the player (probably your model need to be rotated 180 degrees)
    this.entity.lookAt(player.getPosition());
    // move forward (if the rigidbody is kinematic)
    this.entity.translateLocal(0,0,-0.1);
}
1 Like

lol… it says it cant read root property

Can you show me your code please? Also where did you add the code? Is it on the correct place inside the update function, or did you add it random in your script again?

:frowning: i set it as a different script and attached onto the Neighbor an the Eye

i have 2 models

if you want i will get rid of those scripts and add into the update function

If you want a new script then create a new script and don’t remove the code that is in the new script. The initialize function and the update function are almost always required. Place your code inside the update function. I advise you to find out what the initialze function and update function are needed for so you can understand why it doesn’t work this way. Also you need to copy and past all code. Included the last bracket. Right now its missing in your ‘script’. Every bracket that is missing will broke your code.

image

so keep the extra script, but paste it into the Update Function ?

Yes. Better remove this script and make a new one again.

Ok

do i attach this script to the Eye and the Neighbor

I don’t know what is ‘the Eye’, but you have to attach the script to only one entity.

i have an eye entity that follows the player, too