multi-touch

Hello.
I’ve created a topic already, but was unable to do.

I do piano. I did push through OnMouseDown. I need to make a multi-touch. I have tried in different ways, but that does not work, tell me please :slight_smile:

Don’t use OnMouseDown.

Instead in the Update() function you should read your input from the Input.touches[ ] array.

Each Touch object in that array contains everything you need to know about the touches at that particular frame in time.

You would need to correlate that to a particular key using either raycasting into the scene, or perhaps checking the rectangle for a particular piano key.

2 Likes

@Kurt-Dekker Halo sir…
I did it as per ur suggestion…
Could you kindly rectify the problem if any in the code below.
void Update ()
{
if (Input.touchCount > 0)
{
foreach(Touch touch in Input.touches)
{
if(touch.phase==TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay (touch.position);
if(Physics.Raycast(ray,out hit))
{
if(hit.collider.gameObject.tag==“Box1”)
{
hit.collider.gameObject.GetComponent ().pitch = Mathf.Pow (2f, semitone_offset / 12.0f);
hit.collider.gameObject.GetComponent ().Play ();
}
}

}

}
}
}

This somehow doesn’t work…I have tagged the piano key as Box1… I am trying to access the audio source of the piano collider …

Firstly, check this thread for how to post code on the forums for future reference: Using code tags properly

Next, do you get any errors? Does the raycast hit?

My other suggestion would be to use the IPointerClickHandler interface, which entails:

  • adding “using UnityEngine.EventSystems;” at the top of your script.
  • adding the interface (beside ': MonoBehaviour) at the top: “IPointerClickHandler”…
  • letting your IDE help you implement the method for the interface.
  • putting your logic/code inside the method (accessing/changing the audio source).

@methos5k Thanks methos. Will work on that and post if its done. I implemented the OnPointerClick method of IPointerClickHandler… I added a breakpoint at Input.touchCount condition but somehow the condition is not checked and the control exits…

Okay, I forgot to mention a couple of things:

  1. you do not need to check the touch count or phase
  2. you must add a ‘3d graphis raycaster’ to your camera, it’s a component.

Just try printing ‘Debug.Log(“This worked.”)’ to see if the “click/touch” registers. :wink:

Let me know if that works.

Thanks @methos5k . The earlier solution of Ipointerclickeventhandler worked for me…