【发布时间】:2016-11-18 06:42:15
【问题描述】:
我是Unity的新手,我正在用它为Android制作2D游戏,当我按住右键或左键时,它不会继续移动,它只会移动一次,我想做的是当我按住右键或左键时,我希望角色继续移动,直到我松开按钮。有人可以帮忙吗?我将不胜感激!教程是here
这是 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();
}
}
}
}
这是 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();
}
}
}
}
这是 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();
}
}
}
最后,这里是 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);
}
}
【问题讨论】:
-
欢迎来到 Stackoverflow!请不要与我们分享您的所有代码,而只需分享直接影响您的问题的代码。在您的情况下,请仅共享左右按钮的代码。评论how to create a minimal, complete, and verifiable example for more details。
-
我不知道是哪一行代码导致了这个问题。我是 Unity 的新手,我正在学习如何使用它
-
顺便说一句,编程时使用英文变量名是一个好主意。非母语人士更容易理解并帮助您编写代码:)