0
votes

I'm new to Unity, I'm using it to make 2D games for Android, when I hold the right button or left button, it doesn't continue moving , it just moves once, What I want to do is when I hold the right button or left button, I want the character to continue moving until I release the button. Could someone please help? I would greatly appreciate it! The tutorial is here

Here's the code for botonDerScript.cs:

  using UnityEngine;
    using System.Collections;

    public class botonDerScript : MonoBehaviour {

        private PersonajeScript personaje;
        private CircleCollider2D presionar;
        void Start()
        {
            presionar = this.gameObject.GetComponent<CircleCollider2D>();
        }

        // Update is called once per frame
        void Update()
        {
            tocandoPantalla();
        }

        private void tocandoPantalla()
        {
            int numPresiones = 0;
            foreach (Touch toque in Input.touches)
            {
                if (toque.phase != TouchPhase.Ended && toque.phase != TouchPhase.Canceled)
                    numPresiones++;
            }
            if (numPresiones > 0 | Input.GetMouseButtonDown(0))
            {
                Vector3 posicionTap = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2 posicionTap2D = new Vector2(posicionTap.x, posicionTap.y);
                bool presiono = presionar.OverlapPoint(posicionTap2D);
                if (presiono)
                {
                    personaje = this.transform.parent.gameObject.GetComponent<PersonajeScript>();
                    personaje.MoverJugadorDerecha();
                }
            }
        }
    }

Here's the code for botonIzqScript.cs:

using UnityEngine;
using System.Collections;

public class botonIzqScript : MonoBehaviour {

    private PersonajeScript personaje;
    private CircleCollider2D presionar;
    void Start()
    {
        presionar = this.gameObject.GetComponent<CircleCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        tocandoPantalla();
    }

    private void tocandoPantalla()
    {
        int numPresiones = 0;
        foreach (Touch toque in Input.touches)
        {
            if (toque.phase != TouchPhase.Ended && toque.phase != TouchPhase.Canceled)
                numPresiones++;
        }
        if (numPresiones > 0 | Input.GetMouseButtonDown(0))
        {
            Vector3 posicionTap = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 posicionTap2D = new Vector2(posicionTap.x, posicionTap.y);
            bool presiono = presionar.OverlapPoint(posicionTap2D);
            if (presiono)
            {
                personaje = this.transform.parent.gameObject.GetComponent<PersonajeScript>();
                personaje.MoverJugadorIzquierda();
            }
        }
    }
}

Here's the code for PersonajeScript.cs :

using UnityEngine;
using System.Collections;

public class PersonajeScript : MonoBehaviour {

    private JugadorScript[] jugadores;

    void Start () {
        jugadores = GetComponentsInChildren<JugadorScript>();
    }

    // Update is called once per frame
    void Update () {

    }
    public void MoverJugadorIzquierda()
    {
        foreach (JugadorScript jugador in jugadores)
            if (jugador != null) { 
                jugador.moverIzquierda();
            }
    }
    public void MoverJugadorDerecha()
    {
        foreach (JugadorScript jugador in jugadores)
            if (jugador != null) {
                jugador.moverDerecha();
            }
    }
}

And finally, here's the code for JugadorScript.cs:

using UnityEngine;
using System.Collections;

public class JugadorScript : MonoBehaviour
{

    public float velocidad = -10f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void moverIzquierda()
    {
        transform.Translate(Vector2.right * velocidad * Time.deltaTime);
        transform.eulerAngles = new Vector2(0, 0);
    }
    public void moverDerecha()
    {
        transform.Translate(Vector2.right * velocidad * Time.deltaTime);
        transform.eulerAngles = new Vector2(0, 180);
    }
}
1
Welcome to Stackoverflow! Instead of sharing with us all of your code, please just share the code that is directly affecting your problem. In your situation please only share the code for the right and left button. Review how to create a minimal, complete, and verifiable example for more details. - buczek
I don't know which line of code is causing the issue. I'm very new to Unity and I'm learning on how to use it - John Stuz
By the way a good idea when programming is to use English variable names. It's easier for non native speakers to understand and help you with your code :) - Kardux

1 Answers

0
votes

In your botonIzqScript.cs you have the if statement

if (numPresiones > 0 | Input.GetMouseButtonDown(0))
{
    ...
}

Change the | in the conditional to ||. | is a binary operator. || is the "OR" operator.

Do the same in your botonDerScript.cs script

Update

To stop moving when the mouse button is released, add the following if statement:

if (Input.GetMouseButtonUp(0) || (Input.touchSupported && numPresiones == 0))
{
    // stop moving
}