Hi people, I have problems with detecting a click over an object. I found an script and I translated it to C# (the syntax for the raycast gave me some problems, but it’s solved now), but it launchs onRollOver and onPress methods regardless of the mouse position. By the way, elementName is extended from the class GameElement (if someone asks where’s declared). I don’t know what I’m doing wrong, even If someone has another solution for mouse events on a game object it would be awesome.
Sorry If there are spelling mistakes, english isn’t my main lang
Here’s the code.
Greetings
public class InteractiveElement : GameElement {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
mouseActions();
}
void mouseActions()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(!Physics.Raycast(ray, out hit))
{
return;
}
if (!hit.rigidbody || hit.rigidbody.isKinematic)
{
return;
}
onRollOver();
if (!Input.GetMouseButtonDown (0))
return;
onPress();
}
void onRollOver()
{
Debug.Log("Rollover " + elementName);
}
void onPress()
{
Debug.Log("Press " + elementName);
}
}