Why is the lookAt function always backwards? Is there no way to change this? Only for that I have to use a child entity. Not a problem, but such things make my project a bit messy.
You shouldnât have to add another entity. You could just do:
this.entity.lookAt(target);
this.entity.rotateLocal(0, 180, 0);
Thanks for the tip @will! But why is the lookAt function not in the right way? Why there is a turn needed?
I think itâs because when lookAt
was coded, it was assumed that it would be used mainly for cameras to get them to look at certain things in the scene. And PlayCanvas cameras look down the negative Z axis. As it turns out, a lot of people use it to make non-camera entities âlookâ at points in the scene too. Sorry for any confusion or inconvenience with that.
Is it then an idea to create an extra function that does the reverse so that no extra things are needed? (The same is basically moving an object forward with a minus sign and objects backwards without a minus sign. I already understood that this also has to do with the system, but it is more difficult to keep seeing the logic).
Iâm open to suggestions about how to resolve this cleanly and elegantly. Maybe @mvaligursky has some thoughts on this.
PlayCanvas has forward as -Z which is why it seems to be âbackwardsâ . I generally recommend to follow that convention because a lot of functions and properties use that same logic. (Eg. pc.Entity.forward)
If you want to âreverseâ it, you could do a lookAt and rotate around the local Y axis by 180 degrees.
I agree with @yaustar - it seems the behavior is consistent with the engineâs convention.
But as itâs not conventional to have forward to be negative Z, perhaps the lookAt function could take an optional [reverse] parameter, which would need to default to false to not break existing projects.
An optional parameter will be very useful here.
It feels like extra parameter (âreverseâ) to save a single line and add not very flexible functionality is not a best way to do this.
I also considered adding optional Quat âextraRotationâ or Vec3 âaxisâ parameter to allow you to orient any entityâs axis to look at target, but that seems even more complicated solution compared to a single line.
It feels a two line solution @will suggested above is the best - simple interface and complete control.
It is only a single line if you use it once. But if you add up the number of times you use it, it can make quite a difference. It would all remain a bit more organized (at least for me).
You could patch the function or add your own to the entity prototype so it becomes a 1 line function for you.
You can always add a function like this to one of your âglobalâ files:
pc.GraphNode.prototype.lookAtReverse = function(target) {
this.lookAt(target);
this.rotateLocal(0, 180, 0);
}
and then you call it this way:
this.entity.lookAtReverse(target);
Example project of the code above: https://playcanvas.com/editor/scene/904869