[SOLVED] Smooth lookAt function

Oke thank you. That’s looking fine. What is the result of your function right now? Any errors?

I have made it to the point when it rotated only to the first waypoint and it was not updating at all next on, i tried to put the code in many places where it should be but still it rotated only once, i also tried to put it in the update function, so for now i have lookAt function as it was before because i have to test things

Oke, so if I understand you correctly your smooth lookAt function is working now?

Nope, just lookAt, without smoothness is working

Last thing I want to suggest is replacing var playerPosition = this.entity.getLocalPosition(); with var playerPosition = this.entity.getPosition();.

1 Like

Quick suggestion. When the camera enters the sideview, you could make the control for movement go from the up arrow to the right arrow.

1 Like

SOLVED.

special thanks goes to @Albertos

3 Likes

Hi! I’m having the same problem with the smooth look at. I have the following code in update:

var currentPosition = this.entity.getPosition();
var point = this.target.getPosition();
var smoothLookAt = this.rotation.lerp(currentPosition, point, dt * this.velocity);
this.entity.lookAt(smoothLookAt);

Target refers to the player, so when I move the entity should look at me, but smoothly. It doesn’t happen; it looks at me but instantly, not smoothly. I tried to define a lower this.velocity but even with values like 0,0001 does it instantly.

Reading the thread I’m not getting how you solved @smokys and @Albertos . Can you share your final result? How I should modify my code?

Thanks!

What is currentPosition? Is that a copied value or a reference to another vec3?

 var point = this.target.getPosition();

For reference, getPosition() returns a reference to a vec3, not a copy like Unity (as we can’t put objects on the stack in JS). This means if the position of target changes, so does point.

I edited the post.
currentPosition = this.entity.getPosition();

I understand what you say, but I don’t understand yet what am I doing wrong :confused:

var currentPosition = this.entity.getPosition();
var point = this.target.getPosition();

I think the logic is wrong here as this is lerping from the current entity position, in a straight line outwards to the target position which is always going to be the same direction.

It sounds like you want to lerp from where it is currently looking at, towards the target position instead or a rotateTowards function if you want a linear speed rotation.

Hello @PolVegaTPM! Below is what I am doing. If you want to rotate an entity towards a target, all you have to do is set this.moveDirection = this.target.getPosition(); and you can reset it to null when the rotation should stop.

In the initialize function:

this.moveDirection = null;
this.moveDirectionOrginal = new pc.Quat();
this.moveDirectionFinal = new pc.Quat();

In the update function:

if (this.moveDirection) {
    this.moveDirectionOrginal.copy(this.entity.getRotation());
    this.entity.lookAt(this.moveDirection.x, this.entity.getPosition().y, this.moveDirection.z);
    this.moveDirectionFinal.copy(this.entity.getRotation());
    this.entity.setRotation(this.moveDirectionOrginal);
    this.entity.setRotation(new pc.Quat().slerp(this.moveDirectionOrginal, this.moveDirectionFinal, dt*5));
}
1 Like

This project is no longer available

Yes, sorry, it was quite a long time ago, try checking the code posted in the replies above.