- Home /
GUI Button Transform Screenspace
Hey guys,
I'm a little new to scripting, so bare with me: I've made GUI Buttons spawn above a trigger when I enter it. I would like to transform ANOTHER button, above the same trigger, on the X axis via screenspace, not world position. So far I have world position working, but I've no idea how to convert it. I think I need to use WorldToScreenPoint, but I've no idea how.
I would like to control it with the buttonPickupOffset variable, so I can just slap some numbers into the inspector and move it at will.
Here's my code:
var buttonPickupWidth: float = 50;
var buttonPickupHeight: float = 50;
var buttonPickupOffset: float = 0;
var inArea : boolean;
var isButtonPickupVisible : boolean;
var pickedUp : boolean;
var pickupSkin : GUISkin;
function OnTriggerEnter (other : Collider)
{
inArea = true;
Debug.Log("You have Entered");
isButtonPickupVisible = true;
}
function OnTriggerExit (other : Collider)
{
inArea = false;
isButtonPickupVisible = false;
}
function OnGUI()
{
GUI.skin = pickupSkin;
// calculate the world position for the button
var pos3dPickup = transform.position + Vector3.right * buttonPickupOffset;
// convert it to screen space
var posPickup = Camera.main.WorldToScreenPoint(pos3dPickup);
if (inArea == true)
{
posPickup.y = Screen.height - posPickup.y;
if (isButtonPickupVisible == true)
{
if(GUI.Button (new Rect (posPickup.x-buttonPickupWidth/2, posPickup.y-buttonPickupHeight, buttonPickupWidth, buttonPickupHeight), ""))
{
Destroy (gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
WorldToScreenPoint is giving me a wrong X position? 4 Answers
Change GUI button position in code 1 Answer
GUI Button and a gameObject's position 3 Answers
How to set my text to real object 2 Answers
Infinite GUI Button positions. 3 Answers