I have a really good script for switchs, that includes the light that can be turned on, and of, and a sound. The problem with it is that no matter where I click with the mouse, the switch gets activated… Anyone could help me with this problem?, here is my script:
var linkedLight : Light;
var shootSound:AudioClip;
i am assuming your switch is an object in the scene and its been tagged “switch”… then on mouse click you need to do raycast and chech whether you hit the switch…if yes then continue. im going to give c# script… but you get the idea…
function Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100))
{
if(hit.transform.tag == "switch")
{
linkedLight.enabled = !linkedLight.enabled;
audio.PlayOneShot(shootSound);
}
}
}