[SOLVED] lookAt function always backwards

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.

1 Like

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? :sweat_smile:

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.

2 Likes

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. :smile:

1 Like

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.

3 Likes

An optional parameter will be very useful here.

1 Like

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).

1 Like

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);
4 Likes

Example project of the code above: https://playcanvas.com/editor/scene/904869

1 Like