Can't get touch controls to move player properly

Hello all,

So I’m really new to unity and was building a little 2d side scroller game for my andriod phone but I started with getting controls working on the computer which was working out really well, but then I moved it over to the touch controls so it would work on my Andriod phone and this is where things fell apart.

The way I want the controls to work is if the user touches the left or right side of the screen it will just move the character in that direction. The next part of the controls I want working is if the player touches the screen with another figure the character will just jump in the direction it was moving. So here is another example: I would have the phone in my hand sideways like a controller, if my right thumb touches the screen on the right side the character will move in that direction, If I however press the screen anywhere with my left thumb (touch count >1) the character will simply jump in the direction it was going. vice a versa if i’m going in the other direction.

I have tried my hardest to get this to work but it just isn’t working. Here is my code so far:

using UnityEngine;
using System.Collections;

public class KittenControllerv4 : MonoBehaviour
	{
	
	        public float MaxSpeed = 2f;
		bool facingRight = true;
		
		Animator anim;
		
		//check for ground variables
		bool grounded = false;
		public Transform groundCheck;
		float groundRadius = 0.2f;
		public LayerMask whatIsGround;
		public float jumpForce = 700f;
		
		
		// Use this for initialization
		void Start () 
		{
			anim = GetComponent<Animator> ();
		}
		
	    

	    // Update is called once per frame
		void FixedUpdate () 
		{
			grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
			anim.SetBool ("Ground", grounded);
			
			anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
			

			if (Input.touchCount > 0)
			{
				Touch touchtrack = Input.GetTouch (0);
 				if (touchtrack.phase == TouchPhase.Stationary || touchtrack.phase == TouchPhase.Moved)
		     	{  
					float spritePos = transform.position.x;
					if (touchtrack.position.x > Screen.width/2)
					{		
						move = 1;
						anim.SetFloat ("Speed", Mathf.Abs (move));
						rigidbody2D.velocity = new Vector2 (move * MaxSpeed, rigidbody2D.velocity.y);
						if (!facingRight)
						Flip ();
					}
					else 
					{
						move = -1;
						anim.SetFloat ("Speed", Mathf.Abs (move));
						rigidbody2D.velocity = new Vector2 (move * MaxSpeed, rigidbody2D.velocity.y);
					    if (facingRight)
						Flip ();
					}
				}	
			}
			else {
						move = 0;
						anim.SetFloat ("Speed", Mathf.Abs (move));
				}

			
		}
		
	void Update()
	{
		if (grounded  Input.touchCount > 1) 
		{
			anim.SetBool ("Ground", false);
			rigidbody2D.AddForce (new Vector2 (0, jumpForce));
				return;
		}		

		
	}	
	
	void Flip()
		{
			facingRight = !facingRight;
			Vector3 theScale = transform.localScale;
			theScale.x *= -1;
			transform.localScale = theScale;
		}
	}

The only thing I can think of is I was using the rigidbody2d velocity to move the character when it was using keyboard controllers and I might have to go away from that? What currently happens with this code is the player character will run towards which ever way I touch on the screen but the jump always goes in the wrong direction.

Also if there is a best practice, or better way of doing this I have no issues changing the code to that as the screen.width/2 seems like a sloppy way of achieving the end state goal.

Anyhow any help would be greatly appropriated and if this needs to be moved to the 2D forums feel free to do so, I wasn’t sure which forum to post this in.

Thanks,

Hello there

Your coding skills aren’t that bad really. But here’s how I would do it:

void Update()
    {
        if (grounded  Input.touchCount > 1) 
        {
            anim.SetBool ("Ground", false);

            rigidbody2D.AddForce (new Vector2 (0, jumpForce));

           return;
        }       
    }

I would delete this code for now, because I’m going to use it in the fixedupdate since we’re working with rigidbodies.

Now in the fixedUpdate function I’m going through all of the touches and check which one is which finger.

private Touch touch;

    void FixedUpdate () 
    {
        //loop through all of the fingers down on the screen
        for (int i = 0; i < Input.touchCount; i++)
        {
            touch = Input.GetTouch(i);
            switch (touch.phase)
            { 
                case TouchPhase.Began:
                    //if finger started to touch the screen, we check if there's another one down., if so, we know we have to jump
                    if (Input.touchCount > 1  grounded)
                    {
                        Jump();
                    }
                    break;
                case TouchPhase.Stationary:
                    //if its the first finger down, that touch will make it move, all other touches are jumps
                    if (i == 0)
                    {
                        Move();
                    }
                case TouchPhase.Moved:
                    if (i == 0)
                    {
                        Move();
                    }
                    break;
            }
        }
	}
    void Jump()
    {
        anim.SetBool("Ground", false);
        rigidbody2D.AddForce(new Vector2(0, jumpForce));
    }
    void Move()
    {
        float spritePos = transform.position.x;

        if (touch.position.x > Screen.width / 2)
        {
            move = 1;
            anim.SetFloat("Speed", Mathf.Abs(move));
            rigidbody2D.velocity = new Vector2(move * MaxSpeed, rigidbody2D.velocity.y);
            if (!facingRight)
                Flip();
        }

        else
        {
            move = -1;
            anim.SetFloat("Speed", Mathf.Abs(move));
            rigidbody2D.velocity = new Vector2(move * MaxSpeed, rigidbody2D.velocity.y);
            if (facingRight)
                Flip();
        }
    }

I didn’t check for mistakes or compile errors or even gameplay, so I hope this puppy runs from the first time.

Hope it helps

Thanks this seems to work however the only time it seems to fail if you have your finger down on the right side of the screen, then jump and want to jump again after it appears it doesn’t take the stationary hold into consideration anymore and the cat jumps in the other direction again. I’m going to play around with it and see if I can get it to work. It’s the closest its ever been.

On a side note, I’m not sure how or where I can set if nothing is being pressed make move=0; so the animation goes back to the idle status. Previously in my code it was at the end of the if:

 else {

                        move = 0;

                        anim.SetFloat ("Speed", Mathf.Abs (move));

                }

Thanks for the help I will make an update if I get it completely squared away.