If Mouse pressed perform a function.

Hallo. I am trying to create a piano where if the player clicks on it he can roll the mouse through the keys, playing all of the notes the mouse passes through. Now, I got the piano working, but I can’t get the mouse to activate all keys it rolls onto. After hours of attempts, I humbly ask for your help. An yes, I am a noob in this.

Here is the code:

	var note : AudioClip;
var over = 0;
var down = 0;
var mdown : boolean;
mdown = false;

function OnMouseDown () {mdown = true;}
function OnMouseUp () {mdown = false;}

if (mdown == false) {return;}

	function OnMouseOver () {
		if (down==0)
	
									{
					// Perform thingy.
					audio.PlayOneShot (note);
					transform.Rotate(1.8, 0, 0, Space.Self);
					down = 1;
					yield WaitForSeconds(0.2);
					transform.Rotate(-1.8, 0, 0, Space.Self);
					down = 0;}
									}

huttch, would that not only cause it to play once clicked upon? And will this not cause the key to press even if the player misses it?

Ohm perfect! Thank you. I did not study your code correctly :slight_smile: I wonder why I didn’t think of that… Anyway, thanks.

it should work, depends on how your piano's setup, give it a try, if not then ill have a look at your project if you want and help

your welcome

1 Answer

1

have you tried:

function OnMouseOver()
{
  if(Input.GetButton("Fire1")) // Check to see if the player is holding the mouse button down, Fire1 is the default name in the input manager for mouse button 1, if changed then replace it with the name you changed it to
  {
    // play sound and animation code here
  }
}