Your code seems correct reading it out of context, not sure if there is an easier solution given two angles.
When I have two positions (instead of rotations) and I am using lookAt usually I use a tween to do a smooth rotation. Much like this thread we solved together:
Hay @Leonidas, thanks for your reaction! It is actually the same thing as then. However, with the use of Tween I have unfortunately not been able to achieve the good result. In itself, the current way works fine, but I hate the amount of code (and various other functions) it takes.
Player.prototype.updateDirection = function () {
this.lookTo.copy(new pc.Vec3(inputPoint.getPosition().x, this.entity.getPosition().y, inputPoint.getPosition().z));
this.tween = this.app
.tween(this.lookFrom)
.to(this.lookTo, 0.2, pc.Linear)
.on('update', function () {
this.entity.lookAt(this.lookFrom);
}.bind(this))
.on('complete', function () {
this.updateDirection();
}.bind(this))
.start();
};
This is not in a update function
But there are still a few things to solve:
When I increase 0.2 to 0.5 or more, the character will flip in all directions excluding the Y axis, if I move the character at the same time.
I have also a problem with the Y axis when the character is jumping at the same time.
I think it has to do with the fact that the current position of the character is not updated in this tween. Probably because this will only be done when the tween has ended. Is there a solution to that?
Okay, I tried again and then I found out that I don’t even need all the extra functions (at least as it seems now). So this is what it looks like now in the update:
Player.prototype.updateDirection = function (dt) {
if (this.direction !== null) {
this.rotateTime += dt;
this.rotateSpeed = 0.05;
var originalRotation = new pc.Quat();
var finalRotation = new pc.Quat();
originalRotation.copy(this.entity.getRotation());
this.entity.lookAt(this.direction);
finalRotation.copy(this.entity.getRotation());
this.entity.setRotation(originalRotation);
this.entity.setRotation(new pc.Quat().slerp(originalRotation, finalRotation, this.rotateSpeed));
if (this.rotateTime > this.rotateSpeed){
this.direction = null;
this.rotateTime = 0;
}
}
};
When I need to change the rotation of the entity I only have to use something like this:
this.direction = new pc.Vec3(inputPoint.getPosition().x, this.entity.getPosition().y, inputPoint.getPosition().z);
Indeed, Unity has many more options and there is also much more information and tutorials on the internet to realize ideas. Unfortunately I don’t have the knowledge to translate the options Unity offers into code suitable for PlayCanvas.