【问题标题】:Unity game doesn't respond to touchesUnity 游戏不响应触摸
【发布时间】:2017-12-04 20:36:43
【问题描述】:

我有一个 Android 游戏,用户通过触摸屏幕的左侧和右侧来移动玩家。我在Android上尝试了游戏,一切正常。但是有一天它停止了工作:按钮可以工作,但我无法通过触摸屏幕侧面来控制播放器。我尝试重新安装 Unity,删除 Library 文件夹,甚至尝试了其他操作系统。它没有帮助。如何修复我的项目?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MovingPlayer : MonoBehaviour
{

    public float playerSpeed;
    public float maxPos = 2.7f;

    Vector3 position;
    public uiManager ui;
    public AudioManager am;

    public Text coinCount;

    int coin;

    bool currntPlatformAndroid = false;

    Rigidbody2D rb;

    public GameObject particleSystemPrefab;

    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();

#if UNITY_ANDROID
        currntPlatformAndroid = true;
#else
                currentPlatformAndroid = false;
#endif

        am.playerSound.Play();

    }

    // Use this for initialization
    void Start()
    {
        coin = 0;
        position = transform.position;
    }


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

        if (currntPlatformAndroid == true)
        {
            TouchMove();
            //AccelerometerMove();
        }

        else
        {

            position.x += Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;

            position.x = Mathf.Clamp(position.x, -2.7f, 2.7f);

            transform.position = position;
        }

        position = transform.position;
        position.x = Mathf.Clamp(position.x, -2.7f, 2.7f);

        transform.position = position;

    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "CubeObstacle")
        {
            //Destroy (gameObject);
            foreach (ContactPoint2D contact in col.contacts)
            {
                Instantiate(particleSystemPrefab, contact.point, Quaternion.identity);

            }
            //gameObject.SetActive(false);

            GameOverActivated();
            ui.gameOverActivated();
            am.playerSound.Stop();
        }

        if (col.gameObject.tag == "Coin")
        {
            Destroy(col.gameObject);
            coin += 1;
            coinCount.text = coin.ToString();

            Debug.Log("Coin");
        }

    }

    public void GameOverActivated()
    {
        Time.timeScale = 0;
        PlayerPrefs.SetInt("coins", PlayerPrefs.GetInt("coins") + coin);
    }


    void AccelerometerMove()
    {
        float x = Input.acceleration.x;

        if (x < -0.1f)
        {
            MoveLeft();
        }

        else if (x > 0.1f)
        {
            MoveRight();
        }

        else
        {
            SetVelocityZero();
        }
    }

    void TouchMove()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            float middle = Screen.width / 2;

            if (touch.position.x < middle && touch.phase == TouchPhase.Began)
            {
                MoveLeft();
            }

            else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
            {
                MoveRight();
            }
        }
        else
        {
            SetVelocityZero();
        }
    }

    public void MoveLeft()
    {
        rb.velocity = new Vector2(-playerSpeed, 0);
    }

    public void MoveRight()
    {
        rb.velocity = new Vector2(playerSpeed, 0);
    }

    public void SetVelocityZero()
    {
        rb.velocity = Vector2.zero;
    }
}

【问题讨论】:

  • 人们需要查看您的代码才能提供帮助。我们不是读心术的人。
  • 好的。我添加了代码。
  • 看起来只有一帧的阶段是 TouchBegan - 所有其他帧都会调用 SetVelocityZero,取消移动

标签: android unity3d


【解决方案1】:

您不需要使用Input.Touches,这应该可以正常工作:

void Update()
{
    #if UNITY_EDITOR
        position.x += Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;

        position.x = Mathf.Clamp(position.x, -2.7f, 2.7f);

        transform.position = position;
    #else
        if (Input.GetMouseButton(0))
        {
            if (Input.mousePosition.x > Screen.width/2)
            {
                MoveRight();
            }
            else
            {
                MoveLeft()
            }
        }
        else
        {
            SetVelocityToZero();
        }

    #endif
}

希望这会有所帮助:)

【讨论】:

  • 在移动设备上,始终使用Input.Touches。这个问题是关于安卓的。 Input.GetMouseButtonInput.mousePosition 不应用于此目的。
  • 没错。然而,在这个特定的场景中,Input.GetMouseButton(0)Input.MousePosition 工作得很好,因为只允许一个触摸交互来控制/移动左右方向。在其他应该使用“Input.Touches”的情况下。
猜你喜欢
  • 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
相关资源
最近更新 更多