- Home /
OnMouseDown Range Problem
My problem is that I can click on my door to load a new level which is fine but my character can do it from any range which means if he clicks the door from across the map it will still work, how is it I would make it so you have to have a minimum of like 5yards to be in range of making the door work when clicking on it.
Currently: My Character
Door
How is it I would make it say :
My character
Door.
The script I am using;
// Loads the level named "SomeLevel" as a response
// to the user clicking on the object
function OnMouseDown () {
Application.LoadLevel ("Shop 1");
}
Answer by Professor Snake · Mar 07, 2014 at 03:31 PM
You could store a reference to your door object as a variable and then use Vector3.Distance.
var doorObject:Transform;
var neededDistance:float=5;
function OnMouseDown(){
if(Vector3.Distance(transform.position,doorObject)<neededDistance){
Application.LoadLevel ("Shop 1");
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with collecting plants 0 Answers
open a link inside unity 2 Answers
Prefab is not working anymore after opening my project. 3 Answers
Audio when using OnMouseUp 1 Answer