[SOLVED] How to only lookAt X and Z not Y?

So the enemies in my game are zombies that are supposed to chase the player, but I can’t get their models to look in the direction of the player.The reason I don’t want the enemy model to look at the Y is because when the enemy gets close the player they look up and it looks really goofy.
Here is the code…

Enemy.prototype.update = function(dt) {
var target = this.player.getPosition();

// the entity look at player for movement...
this.entity.lookAt(this.player.getPosition());

//model look at player
this.base.lookAt(target.x,0,target.z);

};

Does anyone know how to do this?

For the y axis you can use the position of the entity itself.

this.entity.getPosition().y

I don’t want the Y-axis.
I am trying to only get the base(this.base) to look at the player’s X and Z.

I tried that approach and It didn’t work…

// the base is the model
var target = this.player.getPosition();
    var targetX = this.player.getPosition().x;
    var targetZ = this.player.getPosition().z;

    this.entity.lookAt(this.player.getPosition());
    this.base.lookAt(targetX,0,targetZ);
    

If you don’t want to look at the y axis, you need to use the y position of the entity itself.

What is this.entity and what is this.base?

I don’t see it in your code.

How do you do that? :sweat_smile:

I sent you the link to my project

Because you use a dynamic rigidbody, you can’t use lookAt on the entity itself. Below an example project.

https://playcanvas.com/project/808772/overview/look-at-with-physics

1 Like

Ok so I do understand what you are doing… but how would I do it without a camera?

I am spawning multiple entity enemies and it would crash the game if each had a camera.
Picture4

Something like below.

// the base is the model
var targetPos = this.player.getPosition();
var basePos = this.base.getPosition();

this.base.lookAt(targetPos.x, basePos.y, targetPos.z);

Be aware you need to use the forward direction of your base entity to move the entity forward.

1 Like

It works, thank you so much!

1 Like