x


Distance activated GUI

hi all, i was woundering if there is a way to active a gui object (like a button) when my character get close to it and deactivates when he is far and WITHOUT using colliders (they are just messy and difficult to use, they'll be my last resort).

My game is a space so it has to be able to go everywhere aswell

here is a copy of my GUI code:

var windowRect : Rect = Rect (20, 40, 127, 70);
var icon : Texture2D;
var customSkin : GUISkin;
var target : Transform;
var place1 : Transform;
var place2 : Transform;
var place3 : Transform;
var place4 : Transform;
var place5 : Transform;

function OnGUI () {
GUI.skin = customSkin;

// Register the window. Notice the 3rd parameter
windowRect = GUI.Window (0, windowRect, DoMyWindow, "");
}

// Make the contents of the window
function DoMyWindow (windowID : int) {

GUI.Label (Rect (6, 1, 2500, 20),  "Object In Area");
if (GUI.Button (Rect (10,20,100,20), "Harvest")){
print ("Got a click");
target = place1;
}
if (GUI.Button (Rect (15,40,100,20), "-Moon Mounter")){
print ("Got a click");
target = place2;
}
GUI.DragWindow (Rect (0,0,10000,10000));

}



function Update () {
var targetPoint = target.position;
    var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

}

Thank you in advance, Lachee

more ▼

asked Nov 18 '10 at 04:40 AM

Lachee1 gravatar image

Lachee1
276 7 13 23

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Use the Pythagorean theorem: SqRoot[(x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2]. This gives you the distance between 2 points in 3D-Space. If dist < trigger radius, then activate button. You might not want to do this every frame... but if you do it every half-second/quarter-second or so it won't affect your frame-rate that much...

Example:

var radiusOfButton: float = 20.0;
var playerDist: float;
var pPos: Vector3 = transform.position;
var ePos: Vector3 = enemy.transform.position;

playerDist = Mathf.Sqrt((pPos.x-ePos.x)^2 + (pPos.y-ePos.y)^2 + (pPos.z-ePos.z)^2))

if (playerDist <= radiusOfButton){

    if (GUI.Button(Rect(0,0,50,50),"Button")){
        //Do Whatever
    }

}
more ▼

answered Nov 18 '10 at 12:13 PM

The_r0nin gravatar image

The_r0nin
1.4k 9 14 30

can you give an example?

Nov 19 '10 at 03:42 AM Lachee1

I added a quick example. Note that I only stored the player and enemy positions in another variable for ease of coding (it's not necessary); otherwise I'd have to do (transform.position.x - enemy.transform.position.x), etc.

Nov 19 '10 at 01:15 PM The_r0nin

thank you for helping. I can now use this to advance my gui! (once i debug the previous problem :])

Nov 20 '10 at 10:41 AM Lachee1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3669
x353
x106
x36

asked: Nov 18 '10 at 04:40 AM

Seen: 586 times

Last Updated: Nov 18 '10 at 04:40 AM