Search code examples
c#unity-game-engineinputtouchtouchscreen

Touch screen, how to tweak my code to react to only one finger


I'm trying to add limited touch screen controls to my game for my android phone. I keep seeing all these tutorials but I'm just not getting one of the simple parts, at least i think it's simple. On my phone, when you tap it with your finger, I have a truck that goes left. When I tap it again, I want it to right. Every time you tap the screen, it's supposed to go in the other direction, alternating between left and right. It soooort of works now but not really. When you tap it the first time, it goes left. But in order to make it go right, you have to hold the screen down with one finger and then touch it with the second finger. I just want it to react to only one finger at a time so that you can play it with one hand. So here's my code. What am I doing wrong? How do I fix it? Thank you all in advance.

tried getting rid of the returns. Tried tweaking the existing code and adding other things in there based on other tutorials.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playerController : MonoBehaviour { 

private Rigidbody rb;
private Animator anim;
private bool left = false;
private bool right = false;
public static bool detect = false;
public float yspeed; //left or right from camera's vp
public float xSpeed = 55f; //down the road from camera's vp
[HideInInspector]
public bool crash = false;
public int slowSpeed;
private float respawnTimer = 3f;

void Start () {
    rb = GetComponent<Rigidbody> ();
    anim = GetComponent<Animator> ();
    rb.velocity = new Vector3 (xSpeed, rb.velocity.y, rb.velocity.z);
}


void Update () {    

    Movement ();
    Halt ();
}

public void Movement()
{
    if (crash) {
        left = false;
        right = false;
        scoreManager.scoreValue = 0;
    }
    detect = false;



    if (Input.touchCount > 0 && !left) {
        foreach (Touch touch in Input.touches) {
            rb.velocity = new Vector3 (rb.velocity.x, yspeed, 0);
            left = true;
            right = false;
            return;
        }
    }

        if (Input.touchCount > 0 && !right) {
            foreach (Touch touch in Input.touches) {
                rb.velocity = new Vector3 (rb.velocity.x, -yspeed, 0);
                right = true;
                left = false;
            return;
        }
    }
}

void OnTriggerEnter (Collider other)
{
    if (!crash && other.gameObject.tag == ("wall")) {
        crash = true;
        anim.SetTrigger ("crash");
    }

}
public void Halt()
{       
    if (crash && slowSpeed > 0) {
        rb.velocity = new Vector3 (--slowSpeed, 0, 0);
        Invoke ("Restart", respawnTimer);
    }
}

public void Restart ()
{
    Application.LoadLevel ("scene_01");
}
}

Solution

  • I figured it out and it's so stupid. I just had to update my phone and use fire1 and fire2 as if it were mouse controls, which is what i originally had it as. When I updated my phone, the touchCount arguments didn't react AT ALL to my phone so I tried one of my earlier versions when I was still having it work just for PC. This is the code that works for me now.

    if (Input.GetButtonDown ("Fire1") && !left){
            rb.velocity = new Vector3 (rb.velocity.x, yspeed, 0);
            left = true;
            right = false;
            return;
    
        }
        if (Input.GetButtonDown ("Fire1") && left){
            rb.velocity = new Vector3 (rb.velocity.x, -yspeed, 0);
            right = true;
            left = false;
            return;
        }