- Home /
Question by
stylianos-theodorikakos · Dec 04, 2018 at 06:02 PM ·
scripting problembuttonmousedistancerange
GetMouseButtonDown From a close distant
So i have a button and it can be clicked from anywhere. My question is how can I make it clickable when player is in range like 1 meter or something. Here is my script : using UnityEngine; using UnityEngine.UI; using System.Collections;
public class ButtonOnClickActive : MonoBehaviour {
public GameObject canvas;
public AudioClip search;
private bool clicked;
void OnMouseOver()
{
if(Input.GetMouseButtonDown(0) && !clicked)
{
canvas.SetActive(true);
clicked = true ;
this.GetComponent<AudioSource>().Play();
GetComponent<Renderer>().material.color = Color.red;
}
}
}
Comment
Best Answer
Answer by leftshoe18 · Dec 04, 2018 at 07:15 PM
Change the if statement to include a distance check.
Example :
if (Input.GetMouseButtonDown(0) && !clicked && Vector3.Distance(transform.position, player.transform.position) < 1f)
{
//do stuff
}
Thank you very much that does the work that I wanted!
Your answer
![](https://koobas.hobune.stream/wayback/20220612183332im_/https://answers.unity.com/themes/thub/images/avi.jpg)