1

after searching for a solution I still can't figure out why my multitouch script in unity isn't working. This is my code. And before you ask: All variables do exist.

    void Update()
{
    if (Input.touchCount > 0)
    {
        for (i = 0; i < Input.touchCount; i++)
        {
            if (Input.GetTouch(i).phase != TouchPhase.Ended)
            {
                hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
                if (hit.collider != null && hit.transform.gameObject.tag == "Links")
                {
                    cannon.GetComponent<Rigidbody2D>().MovePosition(cannon.GetComponent<Rigidbody2D>().position + new Vector2(-0.1f, 0) * Time.deltaTime * moveSpeed);
                }
                else if (hit.collider != null && hit.transform.gameObject.tag == "Rechts")
                {
                    cannon.GetComponent<Rigidbody2D>().MovePosition(cannon.GetComponent<Rigidbody2D>().position + new Vector2(0.1f, 0) * Time.deltaTime * moveSpeed);
                }
            }



            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
                if (hit.collider != null && hit.transform.gameObject.tag == "Fire")
                {
                    clone = Instantiate(projectile, cannon.transform.position + new Vector3(0, 1.3f, 0), transform.rotation) as Rigidbody2D;
                    clone.velocity = new Vector2(0, speed);
                }
            }
        }
    }
}

It only registers one input at a time. Yes my phone does support multitouch. I'll appreciate any kind of help.

Klausar
  • 109
  • 1
  • 8

1 Answers1

1

your problem is very simple !

You have a "0" were you should have an "i". That's all it is.

You are looping through with i ...

    for (i = 0; i < Input.touchCount; i++)

sometimes you correctly refer to

     GetTouch(i)

but at other times you incorrectly refer to

    GetTouch(0)

fortunately that's all it is!

Don't forget you can easily solve such problems in the future by logging as you go (use Debug.Log, or, have a Text on the screen and write your development info there, dev.text = "blah" )

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • not at all, just a mistake. doing "touch" is very tricky. Note - you are going to have a problem **that actually, "touch" is deprecated! if you use it, it does not block touches on your buttons and other UI elements. nowadays you have to do this...** long explanation http://stackoverflow.com/q/36048106/294884 – Fattie Mar 21 '16 at 12:05