【问题标题】:Unity Character movement issueUnity 角色移动问题
【发布时间】: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 的新手,我正在学习如何使用它
  • 顺便说一句,编程时使用英文变量名是一个好主意。非母语人士更容易理解并帮助您编写代码:)

标签: c# android unity3d unity5


【解决方案1】:

在您的botonIzqScript.cs 中有 if 语句

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

将条件中的| 更改为||| 是二元运算符。 || 是“或”运算符。

在您的 botonDerScript.cs 脚​​本中执行相同操作

更新

要在释放鼠标按钮时停止移动,请添加以下 if 语句:

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

【讨论】:

  • @JohnStuz 现在您需要检查鼠标按钮或触摸何时被释放并相应地停止移动。我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多