- Home /
Physics.RaycastAll() Get the object the pointer is on!
Hi... it's a bit complicated question... I've a cube which has multiple objects on and inside it... I'm being notified whenever the pointer is on it to return me the object's name. The problem is: Whenever I put the pointer on an object, it returns me it's name... but if I moved the pointer to another object which is on top of it... it keeps showing me the old object name. P.S: I could exchange the [0] with [i], but this will return me the last object always... which is something I don't wish to happen. I only wants the last object closest to the pointer position.
Here is my script:
public LayerMask layermask;
public LayerMask layermask2;
public Text txt;
RaycastHit hit;
public Material[] red_material;
public Material[] normal_material;
public GameObject target;
Stack s = new Stack();
void Start()
{
normal_material = target.GetComponent<Renderer>().materials;
}
void Update()
{
RaycastHit[] hits;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
hits = Physics.RaycastAll(ray, 100.0f, layermask);
txt.enabled = false;
if (hits.Length > 0)
{
hit=hits[0];
if (hits[0].collider.gameObject != target)
{
target.GetComponent<Renderer>().materials = normal_material;
}
}
else
{
target.GetComponent<Renderer>().materials = normal_material;
}
for (int i = 0; i < hits.Length; i++)
{
hits[i].collider.gameObject.transform.position.z)
target = hit.collider.gameObject;
s.Push(i);
target.GetComponent<Renderer>().materials = red_material;
txt.enabled = true;
txt.text = hit.collider.gameObject.name;
txt.transform.position = Input.mousePosition + new Vector3(0f, -30f, 0f);
}
}
Answer by Buckslice · Nov 30, 2017 at 01:09 AM
I think you could just use Physics.Raycast() instead of Physics.RaycastAll()
Tried to... it only returns if the pointer hits a collider or not, it doesn't allow me to return the gameobject.collider name !
Prints name of the hitted object:
print(hits[i].transform.name);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Debug.Log(hit.collider.name);
}
Buckslice
hanks sir, but I'm having a weird problem.... I wrote your exact code, but with a layermask.. it's still returning me a collider though it's on another layer. *P.S: I can't cancel the big collider I'm going through, or make it's layer "IgnoreRaycast"... it's not an option.
For layermask have a public Layer$$anonymous$$ask mask
in your class that you assign in the inspector to the layers you want to hit with the raycast. Then call it like this
Physics.Raycast(ray, out hit, mask.value)
Answer by Multirezonator · Nov 30, 2017 at 01:10 AM
There clearly is not enough some condition:
for (int i = 0; i < hits.Length; i++)
{
hits[i].collider.gameObject.transform.position.z) // error here
target = hit.collider.gameObject;
s.Push(i); // ??
target.GetComponent<Renderer>().materials = red_material;
txt.enabled = true;
txt.text = hit.collider.gameObject.name;
txt.transform.position = Input.mousePosition + new Vector3(0f, -30f, 0f);
}
And for what you keep the indexes of the [for] iterations, what will you do with this stack afterwards? If you what to save some RaycastHit info for other Updates - stack it instead indexes of hits[] array.
I tried to stack them, but stack will simply won't allow me to stack.push a RaycastHit.... how should I do it?
I think about something like this:
s.Push(hits[i]); // ins$$anonymous$$d s.Push(i);
Your answer
Follow this Question
Related Questions
Help doing mouse click changin shader and back to normal to a gameObject ꜛ! 1 Answer
Click listener not working with imported game objects? 2 Answers
How to I get an object to disappear when I use the mouse to click on it? 1 Answer
Restrict mouse click area 3 Answers
How to avoid multiple mouse clicks registering in OnGUI? 1 Answer