Continuous shooting and move left to right from thumb (Unity)

  • iamkunal
    Likes 0

    Problem Description

    I made the game space shooter in Unity and in this game i have a 3 buttons. 1) Left 2) Right 3) Fire but i want the player continuously shoot (fire) and its movement from left to right work with thumb.

    This are the some code which I used in the game.

    shootscript.cs

     

    using UnityEngine;
    using System.Collections;
    
    public class ShootScript : MonoBehaviour {
    
        public int dammage = 0;
        public bool isEnemyShot = true;
        public Sprite lightDestroy;
        public float laserTime = 500;
        private float increament;
    
    
        void Update()
        {
            laserTime += Time.deltaTime;
            if (laserTime <= 0) {
                Destroy(gameObject);
            }
        }
    
    
        void FixedUpdate()
        {
            if (!isEnemyShot) {
                this.transform.Translate (Vector2.up * 8f * Time.deltaTime);
            } else {
                this.transform.Translate (Vector2.up * -3 * Time.deltaTime);
            }
        }
    
        void OnCollisionEnter2D( Collision2D coll)
        {
            if (coll.gameObject.tag == "Player" || coll.gameObject.tag == "Enemy") 
            {
                gameObject.transform.localScale = new Vector2 (1, 1);
                gameObject.GetComponent<SpriteRenderer> ().sprite = lightDestroy;
                gameObject.GetComponent<BoxCollider2D> ().isTrigger = true;
                Destroy (gameObject, 2.0f);
            } else {
                Destroy(gameObject);
            }
        }
    }

    TouchControl.cs

     

    using UnityEngine;
    using System.Collections;
    
    public class TouchControl : MonoBehaviour {
    
        public GUITexture moveLeft;
        public GUITexture moveRight;
        public GUITexture fire;
        public GameObject player;
        private PlayerMovement playerMove;
        private Weapon[] weapons;
    
        void Start()
        {
            playerMove = player.GetComponent<PlayerMovement> ();
        }
    
        void CallFire()
        {
            weapons = player.GetComponentsInChildren<Weapon> ();
            foreach (Weapon weapon in weapons) {
                if(weapon.enabled == true)
                weapon.Fire();      
            }
        }
    
        void Update()
        {
    //      int i = 0;
            if(Input.touchCount > 0)
            {
                for(int i =0; i < Input.touchCount; i++)
                {
    //          if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
    //          {
    //              if(Input.touchCount > 0)
    //              {
    //                  playerMove.MoveLeft();
    

  • iamkunal
    Likes 0

    PlayerMovement.cs

     

    using UnityEngine;
    using System.Collections;
    
    public class PlayerMovement : MonoBehaviour {
        public float speedMove = 3.0f;
        public float bonusTime;
    
        private bool toLeft = false;
        private bool toRight = false;
    
        public GameObject shield;
        public GUIText bonustimeText;
    
        private bool counting = false;
        private float counter;
    
        private Weapon[] addWeapons;
    
        public Sprite strongShip;
        public Sprite normalSprite;
        public Sprite shieldSprite;
    
        private SpriteRenderer sRender;
        private Weapon weaponScript;
    
        void Start () {
    
            counter = bonusTime;
    
            sRender = GetComponent<SpriteRenderer> ();
            addWeapons = GetComponentsInChildren<Weapon> ();
            foreach (Weapon addWeapon in addWeapons) {
                addWeapon.enabled = false;
            }
    
            weaponScript = GetComponent<Weapon>();
            weaponScript.enabled = true;
        }
    
        // Update is called once per frame
        void Update () {
    
            if (Input.GetKeyDown (KeyCode.A)) {
                toLeft = true;      
            }
            if (Input.GetKeyUp (KeyCode.A)) {
                toLeft = false;     
            }
            if (Input.GetKeyDown (KeyCode.D)) {
                toRight = true;     
            }
            if (Input.GetKeyUp (KeyCode.D)) {
                toRight = false;        
            }
    
    
            if (counting) {
                counter -= Time.deltaTime;
                bonustimeText.text = counter.ToString("#0.0");
            }
        }
    
    
        void FixedUpdate()
        {
            if (toLeft) {
                MoveLeft();
            }
    
            if (toRight) {  
                MoveRight();
            }
        }
    
    
        public void MoveLeft()
        {
            transform.Translate(Vector2.right * -speedMove* Time.deltaTime);
        }
    
    
        public void MoveRight()
        {
            transform.Translate(Vector2.right * speedMove * Time.deltaTime);
        }
    
    
        void OnCollisionEnter2D(Collision2D coll)
        {
            if (coll.gameObject.tag == "StrongMode") {
                Destroy (coll.gameObject);
                counting = true;
                StrongMode();
                Invoke (
    																			
  • iamkunal
    Likes 0

    Weapon. cs

     

    using UnityEngine;
    using System.Collections;
    
    public class Weapon : MonoBehaviour {
    
        public Transform laserPrefab ;
        public bool isEnemyWeapon = false;
        public float shootDelay = 1.0f;
        private float waitToShoot;
    
        void Start () {
            waitToShoot = 0f;
        }
    
        void Update () {
    
            if (waitToShoot > 0) {
                waitToShoot -= Time.deltaTime;      
            }
    
            if (Input.GetKeyDown (KeyCode.Space) && isEnemyWeapon == false) {
                Fire();
            }
    
            if (isEnemyWeapon == true && EnemyAttack == true) {
                waitToShoot = shootDelay;
                var shootLaser = Instantiate (laserPrefab) as Transform;
                Vector2 pos = shootLaser.transform.position;
                pos.x = transform.position.x ;
                pos.y = transform.position.y - 0.3f;
                shootLaser.transform.position = pos;
                SoundHelper.instanceSound.EnemyLaserSound();
            }
        }
    
    
        public void Fire()
        {
            var shootLaser = Instantiate(laserPrefab) as Transform;
            Vector2 pos = shootLaser.transform.position;
            pos.x = transform.position.x;
            pos.y = transform.position.y + 0.5f;
            shootLaser.transform.position = pos;
            SoundHelper.instanceSound.PlayerLaserSound();
        }
    
    public bool EnemyAttack
        {
            get
            {
                return waitToShoot <= 0f;
            }
        }
    }
  • Sonar Systems admin
    Likes 0

    What happens at the moment with the code?

  • iamkunal
    Likes 0

    In this code when i am touch the right and left button its move right and left but i want right and left move from my thumbnail (Swipe). and whenever i enter in the game than automatic space ship fire. I don’t want to press the button always for fire. 

  • Sonar Systems admin
    Likes 0

    Swipe?     

Login to reply