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);
}
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);
}
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);
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);
}
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?
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.