- Home /
Question by
webe · Dec 31, 2014 at 10:44 AM ·
2draycasting
Mouseover with Raycast2D doesn't work
I want to highlight my GameObject when the mouse is on it. I got it to work using a 3D collider and 3D rays, but it doesn't work with 2D. The following code does exactly what I want, but in 3D.
Code:
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitInfo;
if (collider.Raycast (ray, out hitInfo, Mathf.Infinity)) {
renderer.material.color = highlightColor;
}
else{
renderer.material.color = normalColor;
}
I've seen plenty of other threads with similar problems, but none of the ones I found worked.
So, how would I do this using Raycast2D?
Comment
Answer by haiderInUnity · Dec 31, 2014 at 01:05 PM
you can do something like this
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition, -Vector2.up);
I had a similar problem 2 days ago and you can refer to my post (http://answers.unity3d.com/questions/864435/how-to-detect-tap-on-object.html)to get a clear idea... there i am just detecting taps for mobile but you can change it to Input.mousePosition