- Home /
Selecting and unselecting objects with raycast
I made this in order to select objects :
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//Debug.DrawRay (ray.origin, ray.direction * 100, Color.yellow);
if (Physics.Raycast (ray.origin, ray.direction, hit, 100))
{
if(hit.collider.tag == "Object")
{
print ("Here is an object!");
}
var object : GameObject = hit.collider.gameObject;
var myAI1 : MyAI1 = object.GetComponent(MyAI1);
//we change the color
myAI1.ChangeColor();
}
It works fine, and i know when the mouse goes over the object, but how could I do to know when the object is being unselected, when the mouse is leaving it?
I mean, I want the object to change color when the mouse is over it. With that code I can only change the color when the raycast enters the object, but not when it leaves. So the object changes, but doesnt return back to the original color.
thank you for all the help!
I fixed your code formatting. PLEASE use the '010101' button to format your code in the future.
not sure I understand your question. Are you asking how to set a boolean to true of false if the raycast is hitting the object?
I want the object to change color when the mouse is going over it. With that code I can only change the color when the raycast enters the object, but not when it leaves.
Answer by aldonaletto · Jun 24, 2011 at 07:38 PM
I think you should use OnMouseEnter and OnMouseExit instead. If the objects have a collider, these functions can do the job. Read about them in:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseEnter.html
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseExit.html
Ok, I will read it but I thought that raycasting was the best way to select objects in 3d space. What do you think I should do? Thnx
These functions already keep track of where the mouse pointer is, and which objects are "under" the pointer, so you should not need to do the raycast yourself. I've not tried them, but according to the docs they seem exactly what you're looking for.
I think you don't need the raycast - at least to select the objects with the mouse. The docs contain examples on how to change the color of a object when the mouse is over it.
Answer by Mischa · Jun 24, 2011 at 06:32 PM
You would have to cache the selected object somewhere. If you just want to change the color back it would be something like that: (untestet code)
////this has to be outside the update loop
var cachedObj : GameObject
////
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//Debug.DrawRay (ray.origin, ray.direction * 100, Color.yellow);
if (Physics.Raycast (ray.origin, ray.direction, hit, 100))
{
if(hit.collider.tag == "Object")
{
var object : GameObject = hit.collider.gameObject;
if(object != cachedObj) //new object selected
{
cachedObj.changeColorBack(); //unselect object
var myAI1 : MyAI1 = object.GetComponent(MyAI1);
myAI1.ChangeColor();
cachedObj = object; //cache the new selected object
}
}
}
It doesnt work
cachedObj.changeColorBack(); //unselect object
Error: changeColorBack() is not a member of unity
I tried to fix it like this
var myAI2 : $$anonymous$$yAI1 = cachedObj.GetComponent($$anonymous$$yAI1); myAI2.changeColorBack();
but now I get another error:
cachedObj = $$anonymous$$yAI1
Error: Cannot convert 'System.Type' to 'UnityEngine.GameObject'
help
changeColorBack() would be a custom function that you add to the $$anonymous$$yAI1 script. Same as changeColor(), but now you should reset it to the old value.
replace cachedObj = $$anonymous$$yAI1 with cachedObj = object. That was my hasty typing, sorry :-)
I edited the code above.
I added the ChangeColorBack() function in $$anonymous$$yAI1, but still the same problem
Error: 'ChangeColorBack' is not a member of 'UnityEngine.GameObject'.
Your answer
Follow this Question
Related Questions
Selecting and unselecting objects with raycast 0 Answers
how to put the objects together (javascript) 1 Answer
Hiding and showing multiple objects 1 Answer
Carrying Objects OnMouseOver 0 Answers
Play sound when clicking object 1 Answer