The object not facing lookAt(target) properly when linearVelocity applied

Hello, someone please help…

I just attached the script to an object so it can move towards the target when Running animation is being played.
But here is the problem… When linearVelocity not applied, the Object facing the desired target correctly, even if I test it with moving the Object with translate() it still facing in the correct direction in realtime.
But when I applied linearVelocity or applyForce, the entity.forward value seems still correct, it still moves to the correct direction, the entity.lookAt(target) still works, but the object angle where it should face was somehow has been reset. I think I’m doing the movement wrong. please, help…

TLDR: the object moves towards the target, but it not facing the target.

Here is the code

ActionController.prototype.move = function(dt) {
    this.entity.lookAt(this.targetPos.x, this.entity.getPosition().y, this.targetPos.z);
    this.forceVal = this.entity.forward.clone().scale(this.entity.speedScale);
    
    switch (this.entity.playerAnim) {
        case 'Idle':
            this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
            break;
        case 'Running':
            this.entity.rigidbody.linearVelocity = this.forceVal;
            break;
    }
};

Hi @alphbuster and welcome,

When an entity has a rigidbody component its movement is fully simulated by the physics engine. That means using setPosition, setRotation or lookAt pc.Entity methods won’t work or have the desired effect.

To set the position and rotation of a rigidbody explicitly you can use the teleport method:

https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#teleport

A good practice is to use a different, child entity, for the character model. That entity will automatically follow the translation of its parent, rigid body, entity. And on that model entity you can use the lookAt method as expected.

2 Likes

Hi @Leonidas, Thank you very much for the reply.
I’ve already tried to separate the child mesh with the rigidbody, but it still didn’t work, it seems my calculation with the lookAt method is not correct.

But since translate still works with lookAt, I assumed rigidbody.teleport should worked too.
And… yes it is working now without separating Base entity (with rigidbody attached) with child mesh.
Again, Thank you very much.

Here is the code…

this.entity.lookAt(this.targetPos.x, this.entity.getPosition().y, this.targetPos.z);
    this.forceVal = this.entity.forward.clone().scale(this.entity.speedScale);
    
    switch (this.entity.playerAnim) {
        case 'Running':
            this.entity.rigidbody.teleport(this.entity.getPosition().add(this.forceVal));
            break;
    }

Hi @alphbuster,

Could you share a sample repro project url to take a look? We can easily debug this in context then, many thanks.

Try to simplify it to identify the issue:

  1. Try to hardcode the height, so instead of this.entity.getPosition().y put some hard number.
  2. If that didn’t help, try to hard code X and Z too, like [0, 1, 0], which should be 1 unit above world center. Does the entity look into that direction?
  3. Add a simple cube to scene and place it at this.targetPos right before you make your entity look at it. Does the cube appear where you expect your entity to look at?