INPUT TOUCH PROBLEMS

When i press left or right i go in the desired way but when i press jump movement stops. If i pres left and walk and press anywhere on the screen the movement gets interupted.
I have tried with layermask but that didnt work to. Do you have any ideas.

using UnityEngine;
using System.Collections;

public class nova: MonoBehaviour {
  
    public GameObject player;
    public bool movingleft = false;
    public bool movingright = false;
    public bool grounded;
    public float jumpForce = 300;
    public int finger1 = -1;
    public int finger2 = -2;
    public int finger3 = -3;
    public Touch touch;
    Rigidbody2D rig;
  
    void Start () {
        rig = GameObject.FindGameObjectWithTag ("player").GetComponent<Rigidbody2D> ();
      
      
    }
  
  
    void Update () {
        grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;
      
      
        if (grounded) {
            //sr.sprite = gor;
        } else if (!grounded) {
            //    sr.sprite = dol;
        }
      
        if (Application.platform == RuntimePlatform.Android) {
            if (Input.touchCount > 0) {
                for (int i=0; i<Input.touchCount; i++) {
                    if(Input.GetTouch (i).phase == TouchPhase.Began){
                        RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero);

                        if(hitInfo.transform.gameObject.name == "levogor"){
                            movingleft = true;
                            finger1 = touch.fingerId;
                        }
                        if(hitInfo.transform.gameObject.name == "desnogor"){
                            movingright = true;
                            finger2 = touch.fingerId;
                        }
                        if(hitInfo.transform.gameObject.name == "jump" && grounded){
                            finger3 = touch.fingerId;
                            rig.AddForce (new Vector2 (0f, jumpForce));
                        }
                    }
                    if(Input.GetTouch (i).phase == TouchPhase.Ended){
                        if(touch.fingerId == finger1){
                            finger1 = -1;
                            movingleft = false;
                        }
                        if(touch.fingerId == finger2){
                            finger2 = -2;
                            movingright = false;
                        }
                        if(touch.fingerId == finger3){
                            finger3 = -3;
                          
                        }
                    }
                }
            }
        }
        if (movingleft) {
            player.transform.Translate (Vector2.left * Time.deltaTime * 5f);
        }
        if (movingright) {
            player.transform.Translate (Vector2.right * Time.deltaTime * 5f);
        }
    }
}

Please put you code in [code ][/code ] tags, as it says in the sticky at the top of every forum here (recopy the code over from your script file when you do, because it loses its formatting when you post without them- it will be just as unreadable if you add them to that mess now, because the tabs were lost).

And please don’t use polls pointlessly, unless you want to add “I’m making it less likely that programmers will help me by making this poll” as the final option on there- then it might get some action.

1 Like

Sorry i’we fixed it :slight_smile: