Klausar
Klausar

Reputation: 119

Can't get multitouch to work in Unity3D

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.

Upvotes: 1

Views: 82

Answers (1)

Fattie
Fattie

Reputation: 12287

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" )

Upvotes: 1

Related Questions