- Home /
Display a button offset an object
Hey Guys,
Have a quick question that I need help with, I'm trying to display a button next to an object in my scene.
I've got the button showing when selected and it's offset correctly to the the object.
My issue is when the screen is rotated the button moves around the object rather then staying the same side.
I know this has to be a screenPos issue but can't figure it out. Thanks
function OnGUI(){
var screenPos = Camera.main.WorldToScreenPoint (transform.position);
if (Selected) {
if (GUI.Button (Rect(screenPos.x-130,Screen.height-screenPos.y,50,30), "Floor")) {
print ("You clicked me!");
}
}
}
Or would anyone recommend using a different method? I was also thinking about using GameObjects as buttons on the four sides of the cube that spawn when clicked.
I guess the best way to describe what i'm trying to do is create plugs on the sides of the game object so when clicked will spawn another game object on that same side.
ScreenPos varies with the screen size so position of button all so varies with the screen size.
Use new UI system for generating the button.
1.create canvas
2.set scale with screen size in canvas scalar script & set screen size.
3.create button and adjust it Anchor to its left top.
if u set screen for landscape the button position remain same for all landscape screen.
$$anonymous$$ake sure you SET the distance between the camera and the gameObject as a parameter in WorldToScreenPoint.
Something like this -
Camera.main.WorldToScreenPoint (Vector3(transform.position.x,transform.position.y, (Camera.main.transform.position.z - transform.position.z ) ));
Your answer
