x


The next problem with gui.

Ok so heres my code from this section:

var Cam : Camera;
public var distanceaway;

function Start()
{
    distanceaway = 0;
}

function OnGUI()
{
        GUI.Label (Rect (10, 20, 100, 20), "distance: "+ distanceaway);
}

function Update()
{
    var ray = Cam.ScreenPointToRay (Input.mousePosition); 
    var hit : RaycastHit; 
    if (Physics.Raycast (ray, hit, 1000)) 
    { 
        distanceaway = hit.distance;    
    }
}

When i run it occasionally it will display the distance but most of the time nothing is displayed after "distance: " just every now and then when scanning the environment itll flash up the distance. Any one got any ideas?

more ▼

asked Feb 17 '11 at 12:12 PM

Kristopher Williams gravatar image

Kristopher Williams
9 4 5 8

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

1 answer: sort voted first

You currently have the ray length set to 1000 units. "Physics.Raycast (ray, hit, 1000)"

Objects without colliders, and objects beyond this distance of 1000 units will not register a collision, and therefore not report a distance. Hope that helps!

EDIT:

After checking out the script in game it looks like it was getting confused casting the number into string. I had the same flashing problem, but changing the GUI call to this:

GUI.Label (Rect (10, 20, 1000, 100), "distance: "+ distanceaway.ToString());

Seems to have fixed the problem, ensuring that distanceaway was interpreted as a string.

more ▼

answered Feb 17 '11 at 12:17 PM

Alec Slayden gravatar image

Alec Slayden
1.2k 1 4 16

the distances involved are all less than 1000. but thanks for the suggestion tho. I appreciate you taking the time to read my problem. just ran it and it flash up at summat like 42.98 and then at like 267.32. lol

Feb 17 '11 at 12:21 PM Kristopher Williams

You're welcome. I think changing the distanceaway to a string through operation will help

Feb 17 '11 at 12:49 PM Alec Slayden
(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:

x3815
x3570
x1200
x373
x319

asked: Feb 17 '11 at 12:12 PM

Seen: 547 times

Last Updated: Feb 17 '11 at 12:21 PM