Mouse Over & Mouse Click

Hi there ppl.

Im having some kind of problem with using RayCast hits and Input.GetButton simultaneosly. Can you please help me ?

What i want to do is a mouse over in a normal gameobject, and a mouse click too in the same gameobject. I tried many things and searched a lot before i post this here, and actually i have this simple comparison :

function Update () {
    var hit : RaycastHit;
    if(Input.GetButtonDown("Fire1")) {
         if(Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance))
              Debug.Log("Mouse DOWN " + hit.point);
    }
}

I correctly get the debug log message saying where my mouse CLICKED …
and if i do this :

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance)) {
          Debug.Log("Mouse OVER " + hit.point);
    }
}

I correctly get the debug log message saying where my mouse IS MOVING. But instead, when i try to use both simultaneosly like this :

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance)) {
          Debug.Log("Mouse OVER " + hit.point);
    }
    if(Input.GetButtonDown("Fire1")) {
         if(Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance))
              Debug.Log("Mouse DOWN " + hit.point);
    }
}

I only get the MOUSE OVER information and never get the information about the click!
Can you please help me understanding what m i doing wrong and whats the best way to achieve what i want ?

Thank you in advance.

You’re not using Raycast correctly.

First, be aware that Unity provides functions doing exactly that, OnMouseXXX() (like Update, it’s called by Unity, you just need to implement it), with some small differences. The script implementing them must be attached to the objects you want to interact with, not to the object casting the ray currently. The ray is cast for all layers except IgnoreRaycast, which can be expensive on some devices and you cannot change it.

About your raycast now. Your rays’ origines are incorrect and yet it’s working, I suspect by luck. The origine must be in world space but Input.mousePosition is in screen space. Also, transform.forward is already local, so applying TransformDirection to it is actually matrix * matrix * (0,0,1), that’s not what you want I suppose. If you don’t want to use OnMouseXXX, try that :

var hit : RaycastHit;
if(Input.GetButtonDown("Fire1")) {
    var ray : Ray = Camera.main.ScreenPointToRay( Input.mousePosition );
    if(Physics.Raycast(ray, hit, distance))
          Debug.Log("Mouse DOWN " + hit.point);
}