when mouse is on a object do this

OK so i need the player to be able to repair an object when they are say 5m away and have their cross hairs (mouse) over the object. i was wondering how i would do this since ive never needed to before. Also if you could answer in c# much appreciated but i can work with java

thanks for any help!

In javascript you can use the function “OnMouseEnter” and “OnMouseExit” to see if the mouse is over the object. And you can use “Vector3.Distance” to measure how far away you are. I think it would look something like this:

//these variables check if we’re the correct distance and if we’re looking at the object, the player variable is for checking distance

var player : Transform;

var looking : boolean;

var distance : boolean;

//these two functions check if the mouse if on the object

function OnMouseEnter () {

looking = true;


function OnMouseExit () {

looking = false;


//this will check if we are the correct distance from the object

function Update () {

if(looking == true && Vector3.Distance(transform.position, player.position) <= 5);


(“repair the object”)




This should go on the object you want to repair, not the player. Hope I helped!

thanks alot helped me out :slight_smile: