hi!
ok i know very little about what im doing…
but at least im trying right?
so i have two objects that i want to be two different buttons basically…
but when i select either button they do the same thing…
so what am i doing wrong here…
this is my whole script attached to the camera…
var hit : RaycastHit;
var button1 : GameObject;
var button2 : GameObject;
function Update () {
button1.gameObject.active = true;
button2.gameObject.active = true;
// Use Raycast to pick objects that have mesh colliders attached.
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object
{
if (Physics.Raycast (ray, hit, 100))
{
button1.gameObject.active = true;
//load something here }
if (Physics.Raycast (ray, hit, 100))
{
button2.gameObject.active = true;
//load something here as well }
}
}
im also getting this error i dont understand…
UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
sorry if this question is insultingly easy!
the conditions in the if statement are exactly the same, so they are both triggered if something gets hit by the ray.
you can use the ‘hit’ variable to determine which object got hit.
regarding that exception, i’m guessing it’s caused by this line:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
just follow his suggestion and do something like
var ray : Ray;
…
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
hey!
thanks for the response!
I changed that line but it seems to be doing the same thing…
i thought that maybe if i added
else if
like this:
else if (Physics.Raycast (ray, hit, 80))
{
button2.gameObject.active = true;
Application.LoadLevel("Level_4a");
}
but that didnt work either.
any ideas here? it seems like something simple…
but obviously not as simple as i am…
haha that was dumb.
just like me.
oh!
You only want to do one RayCast.
If it returns true, look in the Hit structure for what it hit (like hit.collider.name) and base your if/switch statements on that.
would it be possible to show me what you mean?
ive been digging thru the script reference section, and the forum, for days, and i cant seem to make heads or tails out of all the stuff i have found…
sorry to be a pest…
I don’t think you need to be calculating a ray on each frame, just to check when the mouse is down…
also, you might not even need a Ray object, if just to feed it into the raycast…
try this:
var hit : RaycastHit;
var button1 : GameObject;
var button2 : GameObject;
function Update () {
button1.gameObject.active = true;
button2.gameObject.active = true;
if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object
{
if (Physics.Raycast ( Camera.main.ScreenPointToRay (Input.mousePosition), hit, 100))
{
button1.gameObject.active = true;
//load something here }
if (Physics.Raycast ( Camera.main.ScreenPointToRay (Input.mousePosition), hit, 100))
{
button2.gameObject.active = true;
//load something here as well }
}
}
IDK if this might solve your issue, but it is more efficient coding, since you’re only calculating the ray when the raycast is in fact needed.
Hope this helps
Cheers
EDIT:
I just saw you have a hit variable that you’re declaring, but never assigning anything to it… then you pass it as a parameter for the raycast… Is that how it actually works?
Hey HarvesteR!
thanks for that it seems much cleaner than the way i was doing it!
but i still cant seem to get it to actually know when its hitting one object as opposed to the other…
ive been trying to use callahans suggestion of messing with the the hit structure…
but the one word descriptions in the script reference really arent all that helpful for me…
With two gameobjects/colliders named “Level1” and “Level2”
Hit is a return structure, so it has data filled in when a Ray hits something. This is why in C# you have to use out and makes it obvious.
(sorry this is C# but should get the idea)
void Update()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit, 100))
{
Debug.Log("Found " + hit.collider.name);
if(hit.collider.name == "Level1")
{
// Do stuff
}
if(hit.collider.name == "Level2")
{
// Do Other Stuff
}
}
}
}