- Home /
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.
It's pedantic, but the language is called "C#" rather than "C++".
Why did you post a 2nd question on the same topic? The question you had down thread had the code you were working with and the possible solution that I posted. Though I thought you said you had it working for one object in that thread.
yes, i know, and it works fine for just one GameObject, my question is how to put 4 GUIButtons to follow 4 GameObject, one for each gameobject.
cause Vector3 V = Camera.main.WorldToScreenPoint(cube1.transform.position);
i put cube, but that just make work one GUIButton i want 4 GUIButtons.
Apparently the mods haven't allowed my answer to post on the other thread so Ill post it here as a comment. $$anonymous$$y suggestion was to use an array.
public GameObject cube1;
public GameObject cube2;
public GameObject cube3;
public GameObject cube4;
public GameObject cube5;
var objectArray = [cube1, cube2, cube3, cube4, cube5];
var nameArray = ["cube1","cube2","cube3","cube4","cube5"];
void OnGUI()
{
for(i=0, i<objectArray.length,i++){
Vector3 V = Camera.main.WorldToScreenPoint(objectArray[i].transform.position);
if (GUI.Button(new Rect(V.x,Screen.height - V.y,300,200),nameArray[i])) {
Do Something Here;
}
}
}
The above is java script, had to look up how you do arrays in C#. I think you would just replace the array defines thusly:
GameObjects[] objectArray = new GameObjects[5] {cube1,cube2,cube3,cube4,cube5}
string[] nameArray = new string[5[ {"cube1","cube2",...etc]
and it would be i<objectArray.Length the L being capitalized.
I've just put var objectArray = [cube1, cube2, cube3, cube4, cube5]; var nameArray = ["cube1","cube2","cube3","cube4","cube5"];
and says that its wrong..
Answer by IgnoranceIsBliss · Nov 13, 2012 at 01:21 AM
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.
Answer by octaviomejiadiaz · Feb 26, 2013 at 10:50 PM
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"))
}
Your answer
Follow this Question
Related Questions
Detect Click on Gameobject 0 Answers
GUI Button Position - Can it float to the top-left? 3 Answers
How do I make individual buttons change individual variables? 0 Answers
Cube click pop-up menu 0 Answers
GameObject touchable instead GUI button 3 Answers