- Home /
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!
Answer by Posly · Feb 16, 2012 at 08:45 PM
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)
{
("repair the object")
}
}
This should go on the object you want to repair, not the player. Hope I helped!
Your answer
Follow this Question
Related Questions
Destroy object with more clicks depending on Scale? 0 Answers
Don't Load Collected items 1 Answer
I want to put main camera floating 0 Answers
javascript to C#, UnityEngine.Object to GameObject? 1 Answer
Get game component type? 1 Answer