Canvas position based on player position

Hello, so I have a door, and basically what I want is floating text that says “F to interact” but I want that text to appear on the same side that the player is on, but the problem im having is, when I get to the certain point in the doorway the text canvas pops from the left and to the right side at a rapid pace.


So as you can see it works except for, when I am in the doorway it pops back and forward at a rapid pace
You cant see it in the picture but it is going to the left to the right very fast when I am in the doorway

    void Update()
    {
        // if the players position is greater than the doors center put the canvas on the right side of the door
        if ((playerCapsule.position.z - transform.position.z) > 0) {
            TextCanvas.localPosition = new Vector3 (-1, 0, 1);
        }
        // if the players position is any negative value  realitive to the door, position the canvas on the left side of the door
        else {
            TextCanvas.localPosition = new Vector3 (-1, 0, -1);
        }
    }

I have tried a lot of different things including getting a range from 1 to -1 and setting that to not show the text, changing values, and changing the less than sign to less than or equal to, and setting up another if statement for the negative value to move the sign instead of the else, but none of these have worked, Any help is greatly appreciated.

I think you would have to keep track of the last position the text was in. If it was on the left, and the player is on the right, then give it more room before changing instead of zero. Same with on the right, except opposite.

1 Like

I have not toyed with this, but is it possible to just leave the position static and set the draw order so that the ui text is always on top?

Just throwing random in the wind here

1 Like

I would set the position once when the player approaches and you show the text for the first time, rather than setting it every single frame in Update().

1 Like

I put the code in a OnTriggerStay function and its working perfectly now thank you very much.