OnMouseOver and Distance

I want to make a script that when I mouse over a door object and my distance is less than 2 and press the use button the door will play it’s open animation. However i don’t know how to get the distance of the object mousing over the door. How do I do that? I’ve looked at the scripting reference but I couldn’t find what I was looking for. Thanks!

Here is a script that pick objects you can interact with every periode second. This is just off the top of my head, so be careful.

using UnityEngine;
using System.Collections;

public delegate void InteractifEvent( GameObject obj );

public class InteractifPicker : MonoBehaviour
{
    public float period = .1f; // Cast a ray every period second
    public Camera cam;
    public string layerName = "Interactif";
    public float distance = 100f;
    
    public event InteractifEvent onInteractifEnter, onInteractifStay, onInteractifExit;
    
    private bool isPicking = false;
    private GameObject pickedObject = null;
    
    public void StartPicking(){ StartCoroutine( PickingLoop() ); }
    public void StopPicking(){ isPicking = false; }
    
    private IEnumerator PickingLoop()
    {
        isPicking = true;
        while( isPicking )
        {
            Pick();
            yield return new WaitForSeconds(period);            
        }
    }
    
    private void Pick()
    {
        Ray ray = cam.ScreenPointToRay( Input.mousePosition );
        RaycastHit info;
        // First, cast the ray
        if( Physics.Raycast( ray, out info, distance, 1 << LayerMask.NameToLayer( layerName ) ) )
        {
            // Then we check which situation we're in : entering, staying or leaving.
            GameObject obj = info.transform.gameObject;
            if( pickedObject == null )
                RaiseEvent( onInteractifEnter, obj );
            else
            {
                // We switch the target without having a frame with no target.
                if( pickedObject.GetInstanceID() != obj.GetInstanceID() )
                {
                    RaiseEvent( onInteractifExit, pickedObject );
                    RaiseEvent( onInteractifEnter, obj );
                }
                // We're staying on the target
                else
                    RaiseEvent( onInteractifStay, pickedObject );
            }
            pickedObject = obj;
        }
        else
        {
            // We left an object, Exit
            if( pickedObject != null )
                RaiseEvent( onInteractifExit, pickedObject );
            pickedObject = null;
        }
    }
    
    private void RaiseEvent( InteractifEvent e, GameObject obj )
    {
        if( e != null )
            e(obj);
    }
}

And then inside another script

using UnityEngine;

public class PickerTest : MonoBehaviour {
    public InteractifPicker picker;
    
    private void Start()
    {
        picker.onInteractifEnter += new InteractifEvent( Enter );
        picker.onInteractifStay += new InteractifEvent( Stay );
        picker.onInteractifExit += new InteractifEvent( Exit );
        
        picker.StartPicking();
    }
    
    private void Enter( GameObject obj ){ print( "Enter : " + obj ); }
    private void Stay( GameObject obj ){ print( "Stay : " + obj ); }
    private void Exit( GameObject obj ){ print( "Exit : " + obj ); }
}

You can then check the distance in the events.

The way i would look at it is distance from the viewing camera to the gameObject. Then you can specify the distance in a condition.

public var canClick:boolean;
public var someDistance:float;

function OnMouseOver()
{
    var sqrDistance=someDistance*someDistance;
    var distance=Camera.main.transform.position-door.transform.position;
    if(distance.sqrMagnitude<=sqrDistance)canClick=true;
    else if(canClick)canClick=false;
}
function OnGUI()
{
    if(GUI.Button(myRect,"OpenDoor"))
    {
        if(canClick)//open door, play animation
    }
}