- Home /
Question by
PrisVas · Aug 22, 2013 at 12:50 AM ·
c#mousemouseclickmouseover
MouseOver different Objects C#
Hi, In my game the player can click and put the mouse over on lots of objects. I don´t want to attach a class to each object just to control the mouse. I have a Manager class where all objects are declared.
How can I detect mouse is over or clicked using the manager class?
Comment
Have you looked at Raycasting or on$$anonymous$$ouseOver if the objects have a collider
Best Answer
Answer by robertbu · Aug 22, 2013 at 12:57 AM
You use Physics.Raycast(). There are examples in the script reference. Plus lots of examples are posted on UA daily. Here is a bit of code I posted on a question earlier today:
using UnityEngine;
using System.Collections;
public class RaycastExample : MonoBehaviour {
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log ("--------------");
}
}
}
}