2D image follows 3D object

Theory seems nice, however i have no clue how to make that hehe :smile:

I little bit understand that we need to get the front of camera, it has some vector outputs, but how to combine them with object’s position? How to calculate it ?

Here i have some camera forward output

What do you mean by normalized camera position? The current camera getPosition - target.getPosition? can you explain a little bit more in cody way? :slight_smile:

I also use this in the Basic enemy AI, to know if the target is in front of the enemy, but I don’t know how to correct the indicator on screen.

Below my result with using the script of the sample project of @yaustar. It has the same problem as my own script. The indicator is sometimes disorient or flip to the wrong side of the screen. Please play around unil you have seen the problem.

https://playcanvas.com/project/870719/overview/target-indicator

1 Like

Yes thats exactly my issue…i was thinking that i can make somoe workaround, this flip thing always happens when object with marker is way behind the camera, the marker flips from bottom to top… iand i thought that when i detect object with marker is behind camera and marker is flipped i will set marker to bottom by switching coords

1 Like

To know if the target is behind the camera you can simply use the code below.

    if (screenPos.z < 0) {
       console.log("target behind camera");
    }
2 Likes

Thank you it works :)) however im not sure how to switch coords lol, i thought its gonna be easier hah

E: Ok i got it… thankx for help guys, it woks i think :stuck_out_tongue:

image

Unfortunately this solution doesn’t work for me.

Wait, your example you showed me was exactly the same issue I had… how does that not help you ?

I’m not sure if I had the same issue. In your case top and bottom where flipped, in my case it flips to the wrong corner when the target is behind the camera.

If you use the launch mode of my project, you see the result with your fix, maybe it solve a part of the issue and it need some more modification. If you use the play mode you see the result without your fix.

Yes i see, its indeed quite weird… I have the same problem as i have the same code, but my game is other case where this issue wont come up this way. … But its still just poor workaround and it should not be done this way…

The code below solves my problem.

    if (screenPos.z < 0) {
       console.log("target behind camera");
       this.entity.setLocalPosition(-screenPos.x / scale, -(device.height - screenPos.y) / scale, 0);
    }
    else {
        this.entity.setLocalPosition(screenPos.x / scale, (device.height - screenPos.y) / scale, 0);
    }

Now I just need to fix a one frame glitch at the switch point.

Anyone an idea how I can do this?

In Unity / C# tutorials they do this:

    if (screenPos.z < 0) {
        screenPos *= -1;
    }

But this doesn’t seems to work in PlayCanvas / JavaScript.
What is the alternative for it?

Maybe try to check how they define the screenPos variable in C# Unity