Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make character one time shoot in one direction ?

Discussion in 'Scripting' started by konichiwa, Dec 23, 2014.

  1. konichiwa

    konichiwa

    Joined:
    Nov 4, 2014
    Posts:
    5
    Hey everyone,

    I design a character can shoot in four direction: up down left right. By using four button: W S A D. The issue is that , when i push those buttons together the character can shoot in direction at same. But what i want is when i push one button the character only can shoot in that direction .I don`t want my character can shot in different directions at same time. How can i fix this.Please assist .
    This is my code:
     

    Attached Files:

    • Gun.cs
      File size:
      1.1 KB
      Views:
      721
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    One solution could be to use else if (instead of 4 distinct if statements). The user must release the current button to be able to shoot into another direction.
    e.g.

    Code (CSharp):
    1.         if(Input.GetButtonDown("FireRight"))
    2.         {
    3.            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
    4.             bulletInstance.velocity = new Vector2(speed, 0);
    5.         }      
    6.            else if(Input.GetButtonDown("FireLeft"))
    7.         {
    8.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180))) as Rigidbody2D;
    9.             bulletInstance.velocity = new Vector2(-speed, 0);
    10.         }
    11.         else if(Input.GetButtonDown("FireUp"))
    12.         {
    13.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,90))) as Rigidbody2D;
    14.             bulletInstance.velocity = new Vector2(0, speed);
    15.         }
    16.         else if(Input.GetButtonDown("FireDown"))
    17.         {
    18.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,270))) as Rigidbody2D;
    19.             bulletInstance.velocity = new Vector2(0, -speed);
    20.         }
     
  3. konichiwa

    konichiwa

    Joined:
    Nov 4, 2014
    Posts:
    5
    Thanks for ur adivce. I tried ur code but it still can shoot different directions at same time.But ur idea is helpful. So i can make my button like this: disablle other three buttons when i push one.and active those button after i released. But i don`t know the codes which can disable and reactive buttons. If u know , could u teach me that. XD
     
  4. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Could you try

    Input.GetKey

    instead of

    Input.GetButtonDown?
     
  5. konichiwa

    konichiwa

    Joined:
    Nov 4, 2014
    Posts:
    5
    THX GetKey can ......But the frequency is too short .....i try to add some time delay ...Thanks of ur help~XD
     
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    as much as I hate code duplication... a remake of your current script with some fire delay:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Gun : MonoBehaviour {
    5.     public Rigidbody2D bullet;          
    6.     public float speed = 20f;
    7.     public float fireDelay = 0.5f;
    8.     private bool canFire = true;
    9.    
    10.     void Update()
    11.     {
    12.         if(Input.GetButtonDown("FireRight") && (canFire))
    13.         {
    14.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
    15.             bulletInstance.velocity = new Vector2(speed, 0);
    16.             canFire = false;
    17.             Invoke("ToggleFire", fireDelay);
    18.         }
    19.  
    20.         if (Input.GetButtonDown("FireLeft") && (canFire))
    21.         {
    22.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180))) as Rigidbody2D;
    23.             bulletInstance.velocity = new Vector2(-speed, 0);
    24.             canFire = false;
    25.             Invoke("ToggleFire", fireDelay);
    26.         }
    27.  
    28.         if (Input.GetButtonDown("FireUp") && (canFire))
    29.         {
    30.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,90))) as Rigidbody2D;
    31.             bulletInstance.velocity = new Vector2(0, speed);
    32.             canFire = false;
    33.             Invoke("ToggleFire", fireDelay);
    34.         }
    35.  
    36.         if (Input.GetButtonDown("FireDown") && (canFire))
    37.         {
    38.             Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,270))) as Rigidbody2D;
    39.             bulletInstance.velocity = new Vector2(0, -speed);
    40.             canFire = false;
    41.             Invoke("ToggleFire", fireDelay);
    42.         }          
    43.     }
    44.  
    45.     void ToggleFire()
    46.     {
    47.         canFire = !canFire;
    48.     }
    49. }
     
  7. konichiwa

    konichiwa

    Joined:
    Nov 4, 2014
    Posts:
    5
    Thank u very much!