GUI Button follow GameObject, like floating button how to on C++?

Please help, i have being trying to make that a GUI button follow a GameObject, but ive fail in all my tries…

and the other option is to make that GameObject a touchable element.

somebody told me that i can do it by RayCast but i have tried but fail again. cause i dont know where to put some codes…

please help.

If you want a 3D object to follow them, use a GameObject that is a child. Yes, you’ll use a Raycast to detect a click or touch - there are quite a few answers on these forums that will help you figure out how to do it.

Rather than saying ‘Tried and Failed’, send us what you’ve done so we can tell you what may be wrong. You can put the the RayCast code anywhere you want, really - but it’s normally put in the ‘Update’ function of a Component that is attached to the camera.

If you want a GUI.Button to ‘follow’ a 3D object, you’ll need to translate the world-position of the object to a screen-position.

You do that with Camera.WorldToScreenPoint - so for example, you’d have code like this…

void OnGUI()
    Vector3 V = Camera.main.WorldToScreenPoint(FollowingObject.transform.position);

    if (GUI.Button(new Rect(V.x,Screen.height - V.y,100,30),"Button Text");

The first line converts the position of your 3D object into a 2D point location on your screen. The second line draws the button.

sorry to ask again, i’ve lost all my work cause my computer had a problem…
but i’m coding again this thing. and all works fine. but i can’t make that different GUIButtons Follow different cubes(GameObjects).

i have cube1,cube2,cube3,cube4, where do i can put the name of this GameObjects? or what do i have to do?

    void OnGUI()
        Vector3 V = Camera.main.WorldToScreenPoint(cube1.transform.position);		
    		if (GUI.Button(new Rect(V.x,Screen.height - V.y,300,200),"Button Text"))