Thanks in advance. Im new to unity and javascript in general, and this community has been a great resource over the last few weeks while i learn the basics.
So far I have the basics down. I have a FPcontroller with a gui texture crosshair, and a script to turn off the cursor. I have a scene with a ground plane and three boxes
I want to be able to select one of the three boxes with the crosshair and have my gui information updated based on the selection. ie “youve selected box01” i know i need to raycast, but i havent found anything that uses the center of the screen and not the mouse as the raycast origin.
any help would be appreciated, even if its just a push in the right direction, or the bit of code to start searching for.
thanks again
-prestto
I’m in the same boat as you (just learning). Would it be possible to post the script you used for the crosshair and getting rid of the cursor?
As for your question, maybe parent a long cylinder the the camera with no visibility so when it collides with the box it then would select it.
ok, so i figured out how to have the center crosshair constantly update with what has been hit:
but i still dont know how to have this based on a mouse click, not just a mouse over. and how to update a GUI text box with the info.
function Update () {
var ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
print ("I’m looking at " + hit.transform.name);
Debug.DrawLine (ray.origin, hit.point);
} else {
print (“I’m looking at nothing!”);
}
}
for the crosshair i created a 128x128 tiff in photoshop with the alpha channel as the crosshair shape (white is opaque) and the rgb channel as the crosshair color. bring this in as an asset, click on it in the project view, and then go up to gameobject → create other —> GUI texture. This should set the cross hair in the center of the screen. (game view only)
to shut off the cursor. create an empty game object and add this script to it:
Screen.showCursor = false;
noobs helpin noobs…thats what im talking about!
To just have the ray cast when the mouse button is pressed try this-
function Update() {
if(Input.GetButtonDown(“Fire1”)){
var ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
print ("I’m looking at " + hit.transform.name);
Debug.DrawLine (ray.origin, hit.point);
} else {
print (“I’m looking at nothing!”);
}
}
}
I remember watching a long but great tutorial that showed some stuff on updating GUI text with info.
http://www.learnmesilly.com (unity lesson 3 part 4 or 5 i think)
another good one is:
www.ethicalgames.wordpress.com
awesome. that script worked like a charm. thanks.
now for the GUI work. im starting those tutorials now…ill post a solution when i figure it out.