[SOLVED] Make an entity point to another entity?

I want a box to point to my player but when I made the code it did not work. Can anybody help?

var EnemyLookAt = pc.createScript('enemyLookAt');

EnemyLookAt.attributes.add('Player', {type : 'entity'});

// initialize code called once per entity

EnemyLookAt.prototype.initialize = function() {

};

// update code called every frame

EnemyLookAt.prototype.update = function(dt) {

this.entity.lookAt(this.Player);

};

Hi @PhilipJeffrey!

For lookAt you need to use a position, so it should be something like below.

this.entity.lookAt(this.Player.getPosition());

Check also my topic about lookAt.

2 Likes

It didn’t seem to work, huh? Do you know why?

Can you tell what happend instead?

Here is the updated code:

var EnemyLookAt = pc.createScript('enemyLookAt');

EnemyLookAt.attributes.add('Player', {type : 'entity'});

// initialize code called once per entity

EnemyLookAt.prototype.initialize = function() {

};

// update code called every frame

EnemyLookAt.prototype.update = function(dt) {

this.entity.lookAt(this.Player.getPosition());

};

It did not point to the player.

What does it do instead?

It did not point to the player

Sorry, I can’t help you if you don’t provide enough information. I suggest to check the topic I shared in my first reply. Please also note that lookAt doesn’t work with a dynamic rigidbody.

The box is connected to a model do you think that would be the case?

Can you share an editor link of your project please?

Yes : )

Here it is. PlayCanvas | HTML5 Game Engine

Oh wait I figured it out! Thanks for the help!

Great! Can you share your solution for others?