X axis not working with touch controls.

Hello. So today I implemented touch controls to my so far simple 2D game. Shooting works. Melee works. Jumping works… but walking left or right doesn’t. Unity does not give me errors nor I can find them myself. But here is “TouchControls” Script and event triggers of left and right buttons.

void Start () {
        thePlayer = FindObjectOfType<PlayerController>();
    }

    public void LeftArrow()
    {
        thePlayer.Move(-1);
    }

    public void RightArrow()
    {
        thePlayer.Move(1);
    }

    public void UnpressedArrow()
    {
        thePlayer.Move(0);
    }

    public void Sword()
    {
        thePlayer.Sword();
    }

    public void ResetSword()
    {
        thePlayer.ResetSword();
    }

    public void Star()
    {
        thePlayer.FireStar();
    }

    public void Jump()
    {
        thePlayer.Jump();
    }
}

I am very thankful for anyone who can help me. I really want these touch controls but I don’t know scripting enough to fix this problem myself.

2254317--150664--Problem1.JPG
2254317--150665--Problem2.JPG

My guess is that the move method is not working. Maybe that method is supposed to be called repeatedly.

Looks fine- can you write some Debug.Logs and put them in the LeftArrow() RightArrow() functions to tell if those functions are getting called properly? Right now we just know that it isn’t moving.

Copy over the Move() function as well.

Debug says it’s called properly. If I press the touch arrow it shows my debug message. But still not working. I don’t know If it would help but here is my PlayerController script.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float moveSpeed;
    private float moveVelocity;
    public float jumpHeight;

    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool grounded;
    private bool onDGround;

    private bool doubleJumped;

    private Animator anim;

    public Transform firePoint;
    public GameObject ninjaStar;

    public float shotDelay;
    private float shotDelayCounter;

    public float knockBack;
    public float knockBackLengh;
    public float knockBackCount;
    public bool knockFromRight;

    private Rigidbody2D myRigidBody2D;

    public bool onLadder;
    public float climbSpeed;
    private float climbVelocity;
    private float gravityStore;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();

        myRigidBody2D = GetComponent<Rigidbody2D>();

        gravityStore = myRigidBody2D.gravityScale;
    }

    void FixedUpdate() {

        grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
        onDGround = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
    }
  
    // Update is called once per frame
    void Update () {

        if (grounded)
            doubleJumped = false;

        anim.SetBool ("Grounded", grounded);

#if UNITY_STANDALONE || UNITY_WEBPLAYER

        if (Input.GetButtonDown("Jump") && grounded)
        {
            //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
            Jump();
        }

        if (Input.GetButtonDown("Jump") && !doubleJumped && !grounded)
        {
            //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
            Jump();
            doubleJumped = true;
        }

#endif

        //moveVelocity = 0f;

        //moveVelocity = moveSpeed * Input.GetAxisRaw("Horizontal");
        Move (Input.GetAxisRaw("Horizontal"));

        if(knockBackCount <= 0)
        {
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
        } else {
            if(knockFromRight)
                GetComponent<Rigidbody2D>().velocity = new Vector2(-knockBack, knockBack);
            if(!knockFromRight)
                GetComponent<Rigidbody2D>().velocity = new Vector2(knockBack, knockBack);
            knockBackCount -= Time.deltaTime;

        }

        anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));

        if (GetComponent<Rigidbody2D> ().velocity.x > 0)
            transform.localScale = new Vector3 (1f, 1f, 1f);
        else if (GetComponent<Rigidbody2D> ().velocity.x < 0)
            transform.localScale = new Vector3 (-1f, 1f, 1f);

#if UNITY_STANDALONE || UNITY_WEBPLAYER

        if(Input.GetButtonDown("Fire1"))
        {
            //Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
            FireStar();
            shotDelayCounter = shotDelay;
        }

        if(Input.GetButton("Fire1"))
        {
            shotDelayCounter -= Time.deltaTime;

            if(shotDelayCounter <= 0)
            {
                shotDelayCounter = shotDelay;
                //Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
                FireStar();
            }
        }

        if(anim.GetBool("Sword"))
        {
            //anim.SetBool("Sword", false);
            ResetSword();
        }

        if(Input.GetButtonDown("Fire2"))
        {
            //anim.SetBool("Sword", true);
            Sword();
        }

#endif

        if(onLadder)
        {
            myRigidBody2D.gravityScale = 0f;

            climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical");

            myRigidBody2D.velocity = new Vector2(myRigidBody2D.velocity.x, climbVelocity);
        }

        if(!onLadder)
        {
            myRigidBody2D.gravityScale = gravityStore;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "DHGround")
        {
            Destroy (other.gameObject);
        }
    }

    public void Move(float moveInput)
    {
        moveVelocity = moveSpeed * moveInput;
    }

    public void FireStar()
    {
        Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
    }

    public void Sword()
    {
        anim.SetBool("Sword", true);
    }

    public void ResetSword()
    {
        anim.SetBool("Sword", false);
    }

    public void Jump()
    {
        //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);

        if (grounded)
        {
            //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
            //Jump();
            GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
        }
      
        if (!doubleJumped && !grounded)
        {
            //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
            GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
            //Jump();
            doubleJumped = true;
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if(other.transform.tag == "MovingPlatforms")
        {
            transform.parent = other.transform;
        }
    }

    void OnCollisionExit2D(Collision2D other)
    {
        if(other.transform.tag == "MovingPlatforms")
        {
            transform.parent = null;
        }
    }
}

Things that are commented out are mostly bits of code that I used in the past but now are changed.

And how would I do that? Sorry for my knowledge of coding. I barely started. This is my first like proper game not just testing what is possible to do.

It’s supposed to be called every frame that the button is being hold down, and you won’t be able to do it through the event system alone, you’ll have to set a boolean value to true with PointerDown and false with PointerUp, and put “thePlayer.Move(direction);” in the Update function to be called when the boolean is true.

I am so embarrassed but I don’t know how to do that. I mean I can try to figure out something but I bet it won’t work. Could you please help me?

private bool moving = false;
private int direction = 0;

void Update()
{
    if(moving)
        thePlayer.Move(direction);
}

public void LeftArrow()
{
    moving = true;
    direction = -1;
}

public void RightArrow()
{
    moving = true;
    direction = 1;
}

public void UnpressedArrow()
{
    moving = false;
}

Unfortunately that did not work. But I really appreciate your help even if it didn’t work. I guess I’ll scrap the Idea of touch controls. One strange thing that happens is when I switch platforms to Android my jump, melee, shooting buttons don’t work but they work in touch screen. But moving left and right in touch screen doesn’t work but in keyboard it does. Weird.