Can't position a UI element each from if it has an animation attatched

Hello

I have a simple animation attached to my UI Image which makes it go up and down vertically with the rect transform.

But i also have this bit of code in my Update method to position the UI element in a world position so it follows the game object i want it attached to:

public void SetFromGamePosition(RectTransform trans, Vector3 pos)
    {
        Vector2 sp = Camera.main.WorldToScreenPoint(pos);
        trans.position = new Vector3(sp.x, sp.y, 0);
    }

Now if the animation attatched is disabled it moves perfectly fine with the camera, but if i enable it then it won’t work because the two are conflicting with each other. How can i solve for this problem ?

Change your animation code to modify some other variable (called yOffset or something).

Then do this in your update:

trans.position = new Vector3(sp.x, sp.y+yOffset, 0);

I don’t see a y-offset in the rect transform? That or i don’t understand what you meant sorry, could you elaborate :stuck_out_tongue:

How does your animation move the Object and cause it to bounce?

It adjusts the transform rect’s Y axis.

What am asking is HOW it does that… I’m not familiar with using animations to change variables.

My idea was you create your own variable called yOffset. Then however, your animation changes the rectTransform… instead change this yOffset variable. Make yOffset bounce between say -5 and +5 for example. Then in your update code just access this yOffset variables (its one you created not one in Unity) and add it to the Update position:

trans.position = new Vector3(sp.x, sp.y+yOffset, 0);