Search Unity

Best approach for creating a blast wave one a button press in 2D

Discussion in 'Scripting' started by jimm84, Jul 4, 2019.

  1. jimm84

    jimm84

    Joined:
    Feb 26, 2017
    Posts:
    7
    Hello, I've run into bit of sticking point as can't find any of the tutorials online which offer the results i'm after in 2D - or understand.

    • Imagine you are the player from a top down view with enemies and rigidbodies around you.
    • From time to time the player will be able to pick up a EMP power up with limited amount of uses (more on this later)
    • When the player presses the Spacebar I want a shock wave to shoot out from the player - not to far - but relatively powerful. It sends enemies flying.
    • The shock wave only works on the initial jab of the space bar.
    • If you have 3 pick ups you can press space 3 times one after another 1,2,3
    That's essentially it. Doesn't damage, just force on 2D objects like a strong gust of wind that momentary radiates out from the player on the Spacebar press.

    I was wandering what would be the best approach. Creating a CircleCollider that uses its gravity to repel enemies on press (blast enable on space press - blast vanish / enable false after press)?

    Or pure C sharp code. ?

    Thanks for you help, here is my script so far.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAtk : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Update()
    9.     {
    10.         if(Input.GetKeyDown("space"))
    11.          
    12.         {
    13.             Attack();
    14.  
    15.         }
    16.     }
    17.  
    18.     void Attack()
    19.     {
    20.         Debug.Log("blam!");
    21.         // elements of what "Attack" does eg. sends rigid bodies flying,
    22.         // enemies flying in a radius around the player.
    23.  
    24.     }
    25. }
    p.s knock seeking complete answers (unless you want to) more guidance and approach.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Here's some ideas, starting with the easiest:

    - use a ring of particles: expand their size as the ring expands and fiddle with it until it looks right

    - use a ring of sprites in much the same way as above

    - use a LineRenderer and a script that feeds it an ever-expanding ring of points. It sounds complex but it's not much. Once you have the reference to the LineRenderer all you need to do is decide how many points and then run a for loop around the circumference of the circle, expanded by an ever-increasing radius, to set the points.

    - fabricate your own geometry procedurally; this is challenging but extremely straightforward once you grasp the basics. Unity makes procedural geometry generation as easy as it ever could be and I have a package that can show you some hints: https://github.com/kurtdekker/makegeo
     
    jimm84 likes this.
  3. jimm84

    jimm84

    Joined:
    Feb 26, 2017
    Posts:
    7

    //------------
    Hello Kurt, thank you for your ideas.

    Particle blast sounds interesting as does the sprite.

    Regardless of whether I use C-sharp I will need to have an animation show the small emp shockwave. Would it be just as easy to attach a collider that can repel 'all' around it or something and mix that in with a animator?

    The Animator is played/ triggered when the space bar is pressed... if that makes sense?

    I have attached a little picture diagram.

    I'm novice with the code by the way although I have a pinch of experience with it. Thanks
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I left out one obvious solution because of this question being in the Scripting section.

    The most obvious solution is to get out your favorite 3D software package and actually make an animated ring for your shockwave. You could look at some Blender3D tutorials and take a shot making it; I can't imagine it being too difficult but I'm not a 3D artist. :)
     
  5. jimm84

    jimm84

    Joined:
    Feb 26, 2017
    Posts:
    7
    Ha, as simple as that? I wondered if this was worthwhile method.

    Is there a better preferred method to all of the above or just which ever gets the results? I will have crack and drawing an EMP ring and see what happens.

    Just for information or in case any else reads this thread. It's all in 2D.

    Thanks Kurt! :)