[SOLVED] 'lookAt' not working

Hi there.
I’m trying to make when my player (the one in the red square) moves to the another entity in this case the one in the blue square and the idea it’s to look at that entity but it’s not working. Thanks in advance.

Link to the project https://playcanvas.com/editor/scene/1075435

BtnMenu.prototype.onRelease = function (event) {
    //A teletransportarse, ve al getentitydata y pilla el id, si el id es el mismo, manda la posición
    //y de ahí debería teletransportarse
    var id = this.entity.script.btnMenu.id;
    
    for (var i = 0; i < this.EntidadInformaciones.children.length; i++) {
        if(id == this.EntidadInformaciones.children[i].script.entityData.id) {
            
            var position = new pc.Vec3(this.EntidadInformaciones.children[i].position.x,
                this.EntidadInformaciones.children[i].position.y,
                this.EntidadInformaciones.children[i].position.z);
                                  
            this.Player.setPosition(position);                                    
            this.Player.rigidbody.teleport(position);
            
            if(id == this.EntidadAver.children[i].script.entityData.id) {    
                
                var pos = new pc.Vec3(this.EntidadAver.children[i].position.x,
                this.EntidadAver.children[i].position.y,
                this.EntidadAver.children[i].position.z);
                
                
                this.Otro.lookAt(pos);
                this.Player.lookAt(pos);
                this.Player.rigidbody.entity.lookAt(pos);
                console.log("Player ", this.Player);
                console.log("Otro ", this.Otro);
            }  

if it has a rigidbody pc.Entity#lookAt wont work.

2 Likes

Oh I see, Thanks a lot :smile:

As the camera is a child of the player, you would want to use lookAt on the camera entity and not the player.

1 Like

Is this true? Probably you mean a dynamic rigidbody.

2 Likes

Yeah it won’t work as the physics simulation would affect the rotation and effectively overrides the entity’s rotation.

1 Like

It is true I disable the rigid body while moving the entity and worked. Thanks a lot it’s solved it.

1 Like

In my projects, looAt just works as it should on an entity with a rigidbody. So it really depends on how your game is set up and how you use the physics. Also the line below does not seem correct to me.

Anyways, good to know that in your case the solution is to (temporarily) switch off the rigidbody. This solution can help others with the same problem when they want to use lookAt.

lookAt() does indeed work for kinematic rigidbodies. I’m using it right now.
Note that lookAt(entity) does nothing. You have to use the entity’s position, so something like
lookAt(entity.getPosition());

1 Like