mouse to object interaction

hi, i’ve just whipped up this quick script for practise.

function OnMouseOver () {
if(Input.GetMouseButtonDown(0)) {
print ("hi");
}}

however how would i go around possibly using a ray(im not sure) to set a range, so the object is not clickable from one side of the map to another? thanks

simple maths

aVector = camera position - object position

both in world space of course, thats the vector that “connects” the camera and the game object. now you just have to use the resulting vector’s .magnitude attribute to determine how far the object is away from the camera

I use a script like this attached to the game object (a openable door, for example):

var myGuy;

function Start() {  
	  myGuy = GameObject.FindWithTag("Player");
}

function OnMouseOver() { 
	var dist = Vector3.Distance(myGuy.transform.position, transform.position);
        // Check to make sure player isn't too far:
	if(dist < 4.0){
        //  Then do whatever here.
	}
}