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

Question 2 Actions with 1 input

Discussion in '2D' started by so_1899, Mar 19, 2021.

  1. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    Hello Good evening guys ;
    I need help on my code if possible which is :
    I want to make 2 actions with same Button("Fire1")
    my exemple is :


    Script A:
    other.hasexploded = true; // this bool is taken from another script which contains explode fonction
    Update :

    if ( other.hasexploded == true)
    {
    if (Input.GetButtonDown("Fire1"))
    {

    Drop();
    Isdropped = true;

    }

    then on the bombscript

    if (other.Isdropped == true && Input.GetButtonDown("Fire1"))
    {
    Explode();
    hasexploded = true;
    }
    My scripts work correctly but when i press "Fire1" the 2 actions happens on the same time ,
    so my goal is to stop the drop fonction when pressing "Fire1" & the bomb didnt explode i want to drop a bomb unless the first one is already exploded
    to be more clear :
    if input fire1 ====> drop
    if input fire1 again=====> explode only (Not explode + Drop)

    thanks guys for your attention !
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    You are really close. You didnt say what was going wrong and you didnt post your full script so its hard to tell what is wrong, but try this code:

    Code (CSharp):
    1. public class DropClass : MonoBehaviour {
    2.  
    3.     public bool isDropped = false;
    4.     private Explode explodeScript;
    5.  
    6.     private void Start()
    7.     {
    8.         explodeScript = GetComponent<Explode>();
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         if (Input.GetButtonDown("Fire1"))
    14.         {
    15.             if(explodeScript.hasExploded)
    16.             {
    17.                 Drop();
    18.             }
    19.         }
    20.     }
    21.  
    22.     private void Drop()
    23.     {
    24.         //Drop projectile
    25.         isDropped = true;
    26.         explodeScript.hasExploded = false;
    27.     }
    28.  
    29. }
    30.  
    31. public class Explode : MonoBehaviour
    32. {
    33.     private DropClass dropScript;
    34.     public bool hasExploded = false;
    35.  
    36.     void Start()
    37.     {
    38.         dropScript = GetComponent<DropClass>();
    39.     }
    40.  
    41.     private void Update()
    42.     {
    43.         if(Input.GetButtonDown("Fire1"))
    44.         {
    45.             if(dropScript.isDropped)
    46.             {
    47.                 ExplodeFunction();
    48.             }
    49.         }
    50.     }
    51.  
    52.     private void ExplodeFunction()
    53.     {
    54.         //Explode projectile
    55.         dropScript.isDropped = false;
    56.         hasExploded = true;
    57.     }
    58. }
     
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    For the private references to the other scripts, both scripts must be on the same object. My guess is they are not so you can make them public and drag it in. However, since you are creating projectiles and then they explode, the reference will probably become null quickly.

    You will probably have to do some sort of GameObject.Find to get those properly.
     
  4. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    hello buddy i will post my full codes i avoided that bcz it contains more parametres so here is the full code :
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Can you edit your post and repaste your code using code tags ?
    upload_2021-3-22_8-32-20.png
     
    MarekUnity likes this.
  6. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    Script A
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Bombscript : MonoBehaviour
    7. {
    8.     public float fieldofimpact;
    9.     public float force;
    10.  
    11.     public LayerMask LayerToHit;
    12.  
    13.     public GameObject explosioneffect;
    14.  
    15.     public bool hasexploded = false;
    16.    
    17.     public int Timetoautoexplode = 3;
    18.  
    19.     public BOMBDROPPOINT other;
    20.  
    21.     public float waitafterexplosion = 3f;
    22.  
    23.    
    24.  
    25.     void Start()
    26.     {
    27.         other.Isdropped = true;
    28.     }
    29.  
    30.  
    31.     void Update()
    32.     {
    33.  
    34.         if (other.Isdropped == true && Input.GetButtonDown("Fire1"))
    35.         {
    36.             Explode();
    37.             hasexploded = true;
    38.             StartCoroutine(Sometime());
    39.             other.canDrop = true;
    40.             return;
    41.         }
    42.         else
    43.         {
    44.             Invoke("Explode", 3f);
    45.             other.canDrop = true;
    46.         }
    47.  
    48.     }
    49.        
    50.    
    51.  
    52.     public void Explode()
    53.     {
    54.        
    55.        
    56.         Collider2D [] objects = Physics2D.OverlapCircleAll(transform.position, fieldofimpact, LayerToHit);
    57.  
    58.        
    59.         foreach (Collider2D obj in objects)
    60.         {
    61.             Vector2 direction = obj.transform.position - transform.position;
    62.             obj.GetComponent<Rigidbody2D>().AddForce(direction * force);
    63.            
    64.         }
    65.  
    66.         GameObject explosioneffectins = Instantiate(explosioneffect,transform.position, Quaternion.identity);
    67.        
    68.         Destroy(explosioneffectins, 0.2f);
    69.         Destroy(gameObject, 0.1f);
    70.  
    71.        
    72.  
    73.     }
    74.     private void OnDrawGizmosSelected()
    75.     {
    76.         Gizmos.color = Color.red;
    77.         Gizmos.DrawWireSphere(transform.position, fieldofimpact);
    78.      
    79.     }
    80.    
    81.     private void OnCollisionEnter2D(Collision2D col)
    82.     {
    83.         if (col.collider.tag == "Ground")
    84.         {
    85.             GetComponent<Rigidbody2D>().velocity = Vector3.zero;
    86.             GetComponent<Rigidbody2D>().freezeRotation = true;
    87.             GetComponent<Rigidbody2D>().isKinematic = true;
    88.          
    89.            
    90.         }
    91.     }
    92.     IEnumerator Sometime()
    93.     {
    94.         yield return new WaitForSecondsRealtime(0.5f);
    95.     }
    96.  
    97. }
    98.  
     
    Cornysam likes this.
  7. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    Script B
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BOMBDROPPOINT : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Bomb;
    9.  
    10.     public float LaunchForce;
    11.     public Transform Shotpoint;
    12.  
    13.     public GameObject point;
    14.     GameObject[] points;
    15.     public int NumberofPoints;
    16.     public float SpaceBtwPoints;
    17.     Vector2 Direction;
    18.     public int MaxBombs = 2;
    19.     public float Reloadtime = 4f;
    20.     private int CurrentBombs;
    21.    
    22.  
    23.     private bool isReloading = false;
    24.     public bool Isdropped = false;
    25.     public bool canDrop;
    26.  
    27.     public Bombscript other;
    28.  
    29.    
    30.    
    31.    
    32.  
    33.     private void Start()
    34.     {
    35.        
    36.        
    37.         CurrentBombs = MaxBombs;
    38.        
    39.  
    40.         points = new GameObject[NumberofPoints];
    41.         for (int i = 0; i < NumberofPoints; i++)
    42.         {
    43.             points[i] = Instantiate(point, Shotpoint.position, Quaternion.identity);
    44.         }
    45.        
    46.     }
    47.     void Update()
    48.     {
    49.  
    50.         //Points Part
    51.         Vector2 Dropposition = transform.position;
    52.         Vector2 Mouseposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    53.         Direction = Mouseposition - Dropposition;
    54.         transform.right = Direction;
    55.         for (int i = 0; i < NumberofPoints; i++)
    56.         {
    57.             points[i].transform.position = Pointposition(i * SpaceBtwPoints);
    58.         }
    59.  
    60.         //Reload & Drop bombs part
    61.         if (isReloading)
    62.             return;
    63.  
    64.         if (CurrentBombs <= 0f)
    65.         {
    66.             StartCoroutine(Reload());
    67.             return;
    68.  
    69.         }
    70.         if (other.hasexploded == false)
    71.         {
    72.             return;
    73.         }
    74.         else if (other.hasexploded == true)
    75.         {
    76.             canDrop = true;
    77.         }
    78.         if (Bomb.activeInHierarchy)
    79.         {
    80.             canDrop = false;
    81.         }
    82.  
    83.         if (CurrentBombs > 0 && other.hasexploded == true && canDrop == true  )
    84.         {
    85.             if (Input.GetButtonDown("Fire1"))
    86.             {
    87.                
    88.                 Drop();
    89.                 Isdropped = true;
    90.                 canDrop = false;
    91.             }
    92.  
    93.         }
    94.        
    95.  
    96.  
    97.  
    98.     }
    99.        
    100.  
    101.         //couroutine
    102.         IEnumerator Reload()
    103.         {
    104.             isReloading = true;
    105.             Debug.Log("Reloading.....");
    106.  
    107.             yield return new WaitForSeconds(Reloadtime);
    108.  
    109.             CurrentBombs = MaxBombs;
    110.  
    111.             isReloading = false;
    112.         }
    113.         void Drop()
    114.         {
    115.  
    116.             CurrentBombs--;
    117.             GameObject newBomb = Instantiate(Bomb, Shotpoint.position, Shotpoint.rotation);
    118.             newBomb.GetComponent<Rigidbody2D>().velocity = transform.right * LaunchForce;
    119.            
    120.  
    121.  
    122.         }
    123.  
    124.         Vector2 Pointposition(float t)
    125.         {
    126.             Vector2 position = (Vector2)Shotpoint.position + (Direction.normalized * LaunchForce * t) + 0.5f * Physics2D.gravity * (t * t);
    127.             return position;
    128.         }
    129.  
    130.  
    131.  
    132.  
    133. }
    134.  
    135.  
     
    Cornysam likes this.
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    So you pasted more code but you didnt say what the issue is now. What is happening? Is it compiling? Can you place too many objects each click?
     
  9. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    update :
    if i input "fire1" :

    My player is dropping a Bomb

    pressing "Fire1" again :
    Exploding the first One and doesnt sett the Bool (candrop == false)
    and droping another bomb at the same time of the explosion
    i just want to :
    if pressing Fire1 again i want to explode only (not dropping another)
    i want to drop the second bomb while pressing Fire1 3rd Time
    if its not clear tell me i can explain more ;
    thanks for your support buddy , i really apreciate that
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    hmm okay. I think the best thing to do is simplify your 2 scripts, One for handling the spawning and bomb count and one for just exploding and damaging. It will be much cleaner and will be easier to see where issues are arising.

    So your BombObject script will do only these things:
    -Explode with animation
    -Deal damage to player
    -Destroy after explosion

    Your BombController on the enemy should do everything else:
    -Instantiate the projectiles and keep track of the count/timer
    -Determine where the bomb will spawn
    -Etc.

    Basically, we want to get rid of all dependencies from the BombObject to the BombController (all the bools and references should be gone). I will work on a basic template i think will work for this, obviously try it yourself in the meantime
     
    so_1899 likes this.
  11. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    im using exactly what u did except 1 detail : my bomb is not dealing dmg but it doesnt mather
    and im using bombscript on the bomb
    and bomb controller on gameobject called Hand(bombdroppoint)
     
  12. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Code (CSharp):
    1. public GameObject explosionEffect;
    2.     public float timer = 3f;
    3.     public float force;
    4.     public float fieldOfImpact;
    5.     public LayerMask layerToHit;
    6.     private ProjectileController projCont;
    7.  
    8.     void Start()
    9.     {
    10.         projCont = GameObject.Find("Enemy").GetComponent<ProjectileController>();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         timer -= Time.deltaTime;
    16.         if(timer <= 0 || projCont.isDropped)
    17.         {
    18.             Explode();
    19.         }
    20.     }
    21.  
    22.     private void Explode()
    23.     {
    24.         Debug.Log("Exploding projectile");
    25.         //Do all of your current explode code here, it looks fine to me
    26.         Collider2D[] objects = Physics2D.OverlapCircleAll(transform.position, fieldOfImpact, layerToHit);
    27.  
    28.         foreach (Collider2D obj in objects)
    29.         {
    30.             Vector2 direction = obj.transform.position - transform.position;
    31.             obj.GetComponent<Rigidbody2D>().AddForce(direction * force);
    32.         }
    33.  
    34.         GameObject explosioneffectins = Instantiate(explosionEffect, transform.position, Quaternion.identity);
    35.  
    36.         Destroy(explosioneffectins, 0.2f);
    37.         Destroy(gameObject, 0.1f);
    38.         projCont.isDropped = false;
    39.     }
    40.  
    41. }
    42.  
    43. public class ProjectileController : MonoBehaviour
    44. {
    45.     int objectCount;
    46.     public GameObject projectile;
    47.     public Transform shotPoint;
    48.     public float launchForce;
    49.     public bool isDropped;
    50.  
    51.     void Start()
    52.     {
    53.  
    54.     }
    55.  
    56.     void Update()
    57.     {
    58.         if(Input.GetButtonDown("Fire1"))
    59.         {
    60.             if(isDropped != true)
    61.             {
    62.                 DropProjectile();
    63.                 //We set isDropped to False in the Exlode function on the Projectile but the instant it goes true (within DropProjectile) it will set back.
    64.             }
    65.         }
    66.     }
    67.  
    68.     private void DropProjectile()
    69.     {
    70.         Debug.Log("We are dropping the projectile");
    71.         GameObject newObj = Instantiate(projectile, shotPoint.position, shotPoint.rotation);;
    72.         newObj.GetComponent<Rigidbody2D>().velocity = transform.right * launchForce;
    73.         isDropped = true;
    74.     }
     
    so_1899 likes this.
  13. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    So i couldnt get rid of all dependencies since we are looking to control when it goes off. But i think this will get you what you need and is much more concise than what you had. Obviously add in your OnDrawGizmos stuff that wont affect the functionality.

    I think your issues lies within:
    upload_2021-3-22_13-27-56.png

    And

    upload_2021-3-22_13-28-19.png

    You were setting canDrop many different places instead of One place for True, and one place for False. It can just get really messy when you do that.
     
    so_1899 likes this.
  14. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    What should i do ? :D
     
  15. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Use the code i sent one message before that one you just replied to
     
  16. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    y mb i didnt saw,
    i just used your code and it does the same behaviour as mine with no diff ;
    maybe my bools was kinda confusing but they did the job tho :D
    So i still have the same problem :(
    i want to use the input only for 1 action either drop or explode
    thanks again
     
  17. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Unfortunately i cannot really help much unless i created a sample project to try it out and i dont have the time right now. Throw in some more debug.logs everywhere and try to find what isnt working. Figure out when certain bools are true/false, when you click, etc. Make sure every float and variable is set properly in the inspector. You may even have to create new gameobjects and just re-type some scripts.

    If you try the code sample i gave and both the Debug.Logs of "Dropping" and "Exploding" are being called AND not at the same time, then the code works and something else is the issue.

    Good luck!
     
    so_1899 likes this.
  18. so_1899

    so_1899

    Joined:
    Mar 19, 2021
    Posts:
    11
    imma keep searching for the solution thanks a lot for your time and i wish u a great night ;)