【问题标题】:unity - android touch button with name fire1 not workingunity - 名称为fire1的android触摸按钮不起作用
【发布时间】:2018-01-05 00:14:42
【问题描述】:

我需要帮助来修复以下代码,以便在触摸开火 1 按钮时能够开火,当我在手机上使用统一遥控器时,它不起作用

步骤

  1. 将 PlayerShoot 脚本关联到 GameObject
  2. 单击场景中的按钮,转到检查器。并添加一个 OnClick 事件,拖动上面有脚本的游戏对象,然后选择 Fire() 函数。
  3. 在启动按钮不起作用的移动设备上使用统一远程应用程序

我使用的代码...如下:

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

    public class PlayerShoot : MonoBehaviour {

        public GameObject Ammo; // the shot
        GameObject FiredShot;
        public AudioClip bcgMusic;
        public List<GameObject> ShotsOnAir;

        //public AudioClip sound1;    // Use this for initialization

        void Start (){
            ShotsOnAir = new List<GameObject>();
        }

        //void Update (){
        //    Fire ();
        //}

        public  void Fire(){
            if (Input.GetButtonDown ("Fire1")){

                FiredShot = (GameObject)Instantiate(Ammo,transform.position,transform.rotation);
                ShotsOnAir.Add(FiredShot);
                AudioSource.PlayClipAtPoint (bcgMusic, transform.position);
            }

            if(ShotsOnAir != null){
                int i=0;

                foreach (GameObject GB in ShotsOnAir){


                    ShotsOnAir[i].transform.position += ShotsOnAir[i].transform.forward * 200 * Time.deltaTime;

                    i++;
                }

            }

        }

    }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    Input.GetButton() 在移动设备上不起作用

    您需要改用Input.touches/Input.GetTouch()

    【讨论】:

      【解决方案2】:
      using UnityEngine;
      using System.Collections;
      using System.Collections.Generic;
      
      public class PlayerShoot : MonoBehaviour {
      
          public GameObject Ammo; // the shot
          public GameObject FiredShot;
          public AudioClip bcgMusic;
          public List<GameObject> ShotsOnAir;
      
          //public AudioClip sound1;  // Use this for initialization
      
          void Start (){
              ShotsOnAir = new List<GameObject>();
          }
      
          //void Update (){
          //  Fire ();
          //}
      
          public  void Fire(){
              //if (Input.touches ("Fire1")){
      
              if (Input.touchCount > 0 ){         
                  FiredShot = (GameObject)Instantiate(Ammo,transform.position,transform.rotation);
                  ShotsOnAir.Add(FiredShot);
                  AudioSource.PlayClipAtPoint (bcgMusic, transform.position);
              }
      
              if(ShotsOnAir != null){
                  int i=0;
      
                  foreach (GameObject GB in ShotsOnAir){
      
      
                      ShotsOnAir[i].transform.position += ShotsOnAir[i].transform.forward * 10000 * Time.deltaTime;
      
                      i++;
      
                  }
      
      
              }
      
          }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2022-10-18
        • 2015-10-05
        • 1970-01-01
        • 2019-08-15
        • 1970-01-01
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多