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);
}
}