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 Is this problem solvable?

Discussion in 'Scripting' started by SleepyWolfie, Aug 7, 2023.

  1. SleepyWolfie

    SleepyWolfie

    Joined:
    Jun 8, 2020
    Posts:
    28
    Let's say I have a character with a slingshot. I want the character to fire a bomb with the slingshot with a cooldown between bombs. The character can move very quickly in space.
    I also have input button H.

    1. If I press H (GetButtonDown("H")), I leave the bomb where Im standing.
    2. If I keep H pressed (GetButton("H")), I can charge the bomb on the slingshot, If I keep it pressed for longer I can send it further and further away.

    Is this possible in any way? Because 2 is gated by 1, so I will leave the bomb first, triggering the cooldown and not being able to hold it, since they are triggered by the same button.

    The only way I see that I can solve this is by making 1 be triggered by button release (GetButtonUp("H")), but I want the snappiness and instant action of just pressing the button and leaving the bomb immediately, since our character moves very quickly I want to be able to leave the bomb precisely where I wanted when I pressed the button.

    The second way I see of solving this is by introducing a second input button J, but now the actions are deferred in a strange way, It'd be great to have it all on input H.

    The bomb and slingshot is just an analogy to my game's idea which is much more complex and would just complicate the question.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This is the way I do it... you'd be surprised at how snappy it feels.

    The only other way is to tentatively place the bomb, but then if your finger remains down for more than what you consider a tap, you pick it back up and start pulling the slingshot back.

    Are you mashing up Angry Birds with Bomberman? :)
     
    Bunny83 likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.     void Thrower()
    2.     {
    3.         if (Input.GetMouseButton(0))
    4.             force+=0.01f;
    5.         else if (Input.GetMouseButtonUp(0))
    6.         {
    7.             GameObject bomb=Instantiate(bombPrefab,transform.position,transform.forward,Quaternion.identity);
    8.             bomb.GetComponent<Rigidbody>().velocity=transform.forward*force;
    9.             force=1f;
    10.         }
    11.     }
     
    Last edited: Aug 7, 2023
  4. SleepyWolfie

    SleepyWolfie

    Joined:
    Jun 8, 2020
    Posts:
    28
    Ill try this out, see if its as snappy as we want it :)

    No no mashup, its just an analogy to our idea hehe (not a bad one though!)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Speaking of Bomberman... the way the "throw bomb" powerup (also known as grab) worked was that if you were precisely on a spot where a bomb had been placed, yours or otherwise, and you had the grab powerup, if you tried to drop again, the bomb was picked up and held overhead... the timer didn't stop so it would happily blow up in your hands if you didn't turn, face and release the thing somewhere safe, like over a brick wall into someone else's space.

    So that's another angle: drop and physically re-pick up by a second tap -> hold.
     
    DragonCoder likes this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    More like this:

    Code (CSharp):
    1.  
    2.         float tapDelay = 0.5f;
    3.         void Thrower()
    4.         {
    5.             if (Input.GetMouseButton(0))
    6.                 force += Time.deltaTime;
    7.             else if (Input.GetMouseButtonUp(0))
    8.             {
    9.                 Vector3 velocity = Vector3.zero;
    10.                 if (force > tapDelay)
    11.                     velocity = transform.forward * (force-tapDelay);
    12.                 force = 0f;
    13.                 GameObject bomb=Instantiate(bombPrefab, transform.position, Quaternion.identity);
    14.                 bomb.GetComponent<Rigidbody>().velocity = velocity;
    15.             }
    16.         }
    17.  
    In principle we could get rid of the GetMouseButtonUp check and replace it with (force > 0). This would have the same effect. The nested if statement determines the difference between a tap and a charge. So if you hold down longer than "tapDelay" you start charging and that time is used to scale the velocity. Note I subtracted the tapDelay from the "time" so the charging actually starts at 0. Otherwise when you want to change the tapDelay for balancing reasons, the minimum charging strenth would change with it which is not great. If you decide that you want to have a 1 second delay, you could not launch it any slower than 1ups. You can re-introduce a constant "min" force so the user can not release with almost 0 force like that:
    Code (CSharp):
    1. velocity = transform.forward * (minForce + force - tapDelay);
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    omg! that's bloatware! :eek:






    just kidding..
     
  8. SleepyWolfie

    SleepyWolfie

    Joined:
    Jun 8, 2020
    Posts:
    28
    Thats actually quite an interesting angle! I never thought about that one (been a while since I played Bomberman heh), thanks for the input!
     
  9. SleepyWolfie

    SleepyWolfie

    Joined:
    Jun 8, 2020
    Posts:
    28
    As for the other answers relating to throwing an actual object, thanks! But the "throwing" of a bomb is an analogy for another different idea. I've explored the possibility of charging up a float variable where 0 is "dropping" and anything above is different strengths of throwing. The problem lies that in my project the "button up" approach is not as snappy as I'd hope. I'm gonna try in an empty project to see if its something about my games framework.