Sa'ad Qasaymeh
Sa'ad Qasaymeh

Reputation: 35

unity - android touch button with name fire1 not working

i need help to fix the following code to be able fire when bullet when touch the fire 1 button its not working when i use unity remote on my mobile

steps

  1. Assined the PlayerShoot script to GameObject
  2. click on the button in the scene, go to the inspector. and add an OnClick event, drag the gameobject that has the script on it, and select the Fire() function.
  3. using the unity remote app on mobile where the fire button not working

the code i use ...as the following :

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

            }

        }

    }

Upvotes: 0

Views: 2731

Answers (2)

Sa&#39;ad Qasaymeh
Sa&#39;ad Qasaymeh

Reputation: 35

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++;

            }


        }

    }


}

Upvotes: 0

Input.GetButton() does not work on mobile

You need to use Input.touches/Input.GetTouch() instead

Upvotes: 1

Related Questions