unity 3d student

Worms Style Power Bar


Following on from the Football kick power bar example, I was asked by reader Adam to provide an example that shows off shooting by charging up power rather than timing, so I present this example script for your perusal! as usual throw me any questions you may have about it. To make things simpler, i’ve also uploaded the script here as some lines are too wide to fit this site’s column effectively- http://www.pasteit4me.com/2050001

Video Explanation

//Script by Will Goldstone at Unity3dstudent.com
  1.  
  2. //set the maximum width of the bar to be used for the maths function below
  3. var fullWidth : float = 256;
  4.  
  5. //create a private variable (not shown in inspector) to store the current set power
  6. private var thePower : float;
  7.  
  8. //create a boolean flag we can use to stop and start the addition to power
  9. var increasing : boolean = false;
  10.  
  11. //create a boolean flag we can use to stop the player shooting during the shot
  12. var shooting : boolean = false;
  13.  
  14. //speed to increment the bar
  15. var barSpeed : float = 25;
  16.  
  17. //create a slot to assign my cannonball prefab to.
  18. var ball : Rigidbody;
  19.  
  20. //create a blast particle slot to assign particle emitter to
  21. var blastPart : ParticleEmitter;
  22.  
  23. //create a light slot to assign a light prefab for when the blast occurs
  24. var cannonLight : Light;
  25.  
  26. //create a slot to assign an empty game object as the point to spawn from
  27. var spawnPos : Transform;
  28.  
  29. // create a number to multiply the force by as the value of up to 256 may not be enough to
  30. // effectively shoot a ball forward
  31. var shotForce : float = 5;
  32.  
  33. //a prefab of some crates to shoot at
  34. var crates : GameObject;
  35.  
  36. //audio for the blast
  37. var cannonBlast : AudioClip;
  38.  
  39. //create a private variable to store currently created stack of crates
  40. private var currentCrates : GameObject;
  41.  
  42. function Start(){
  43.  //set the power bar to zero at start
  44.  guiTexture.pixelInset.width = 0;
  45.  //create the target crate stack, and set currentCrates variable to represent this stack
  46.  var someCrates : GameObject = Instantiate(crates, Vector3(10, 15, -8), transform.rotation);
  47.  currentCrates = someCrates;
  48. }
  49.  
  50. function Update () {
  51.  
  52.   //if we are not currently shooting and Jump key is pressed down
  53.   if(!shooting && Input.GetButtonDown("Jump")){
  54.    //play the sound set on the audio source
  55.    audio.Play();
  56.    //set the increasing part of Update() below to start adding power
  57.    increasing=true;
  58.   }
  59.  
  60.   // detect if Jump key is released and then call the Shoot function, passing current
  61.   // value of 'thePower' variable into its 'power' argument
  62.   if(!shooting && Input.GetButtonUp("Jump")){
  63.    //reset increasing to stop charge of the power bar
  64.    increasing = false;
  65.    //call the custom function below with current value of thePower fed to its argument
  66.    Shoot(thePower);
  67.   }
  68.  
  69.   if(increasing){
  70.    //add to thePower variable using Time.deltaTime multiplied by barSpeed
  71.    thePower += Time.deltaTime * barSpeed;
  72.    //stop (or 'fight') thePower from exceeding fullWidth using Clamp
  73.    thePower = Mathf.Clamp(thePower, 0, fullWidth);
  74.  
  75.    //set the width of the GUI Texture equal to that power value
  76.    guiTexture.pixelInset.width = thePower;
  77.  
  78.    //set the pitch of the audio tone to the power var but step it down with division
  79.    audio.pitch = thePower/30;
  80.   }
  81. }
  82.  
  83. // start the 'Shoot' custom function, and establish a
  84. // float argument to recieve 'thePower' when function is called
  85. function Shoot(power : float){
  86.  //stop shooting occuring whilst currently shooting with this boolean flag
  87.  shooting  = true;
  88.  
  89.  //create a particle burst
  90.  var pBlast : ParticleEmitter = Instantiate(blastPart, spawnPos.position, spawnPos.rotation);
  91.  //base blast amount on power argument, and divide it to diminish power
  92.  pBlast.maxEmission = power/4;
  93.  
  94.  //create a light to act as a flash for the blast, base its range & intensity
  95.  //upon the power variable and destroy it after 0.1 seconds
  96.  var canLight : Light = Instantiate(cannonLight, spawnPos.position, spawnPos.rotation);
  97.  canLight.intensity = power/7;
  98.  canLight.range=power/7;
  99.  Destroy(canLight.gameObject, 0.1);
  100.  
  101.  //stop the audio source on this object to cut off the tone build up to launch
  102.  audio.Stop();
  103.  
  104.  //play the sound of the cannon blast in a new object to avoid interfering
  105.  //with the current sound assignment and loop setup
  106.  AudioSource.PlayClipAtPoint(cannonBlast, transform.position);
  107.  
  108.  //create a ball, assign the newly created ball to a var called pFab
  109.  var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);
  110.  
  111.  //find the forward direction of the object assigned to the spawnPos variable
  112.  var fwd : Vector3 = spawnPos.forward;
  113.  pFab.AddForce(fwd * power * shotForce);
  114.  Destroy(pFab.gameObject, 4);
  115.  
  116.  //pause before resetting everything
  117.  yield WaitForSeconds(4);
  118.  
  119.  //reset the bar GUI width and our main power variable
  120.  guiTexture.pixelInset.width = 0;
  121.  thePower = 0;
  122.  
  123.  //destroy the existing crates and spawn a new stack
  124.  Destroy(currentCrates);
  125.  var someCrates : GameObject = Instantiate(crates, Vector3(8, 15, -11), transform.rotation);
  126.  currentCrates = someCrates;
  127.  
  128.  //allow shooting to occur again
  129.  shooting = false;
  130. }

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

28 Comments

    absolutely amazing! just what i wanted, thank you Will for such a speedy reply and to get a working demo up so soon too. I’m sure many others will find this very easy to understand and helpful as well.

    my hats off to you sir!

    Adam :)

  • Hmm , that sound nearly blasted my ears. It keeps going up and I had my headset on for something that was set to about 10% volume giving this one a big blast. If you don’t want to be deaf. Click the top right button.

    P.S. if you keep the shoot key pressed and release after reset you shoot with no power. Might wanna recode that if you copy this for usage.

  • ahh you got a video up too, great! :) Oh and i just wanted to add you are a lucky man to have R-Type arcade machine, 1 of the defining games of my youth.

    speaking of retro, wouldn’t it be great to see altered beast done in 3d as a 3rd person multiplayer game, i personally think the concept of your player character metamorph once several power ups are gained would give a good replay factor to gamers.

    Anyway thanks again for getting this wealth of info up so soon for us all to learn from. ;)

  • this code very useful for me!!!
    but It’s too difficult to fix.. help me..

    I just shoot player_collider(GameObject)

    but when play unity,
    ————————
    player_collider(clone)
    player_collider(clone)
    player_collider(clone)
    player_collider(clone)
    player_collider(clone)(clone)
    player_collider(clone)(clone)
    player_collider(clone)(clone)
    ……
    ——————————

    I dont know what the probelm…

    please help me

    private var startPlayer : temp; //temp script link
    //create a boolean flag we can use to stop the player shooting during the shot
    var shooting : boolean = false;
    var playerAngle : float = 0; //player Angle Initialization
    var playerPower: float = 0; //player Power Initialization
    var ShootPower : float = 50; //shoot speed

    //create a slot to assign an empty game object as the point to spawn from
    var spawnPos : Transform;

    var player_collider : Rigidbody ;

    startPlayer = FindObjectOfType(temp);

    function Update ()
    {
    shooting = startPlayer.allTemp; //true receive temp
    playerAngle = startPlayer.inputAngle; //angle receive temp
    playerPower = startPlayer.inputPower; //speed receive temp
    if(shooting !=false)
    {
    Shoot(playerPower);
    }
    }

    function Shoot(power : float){
    //stop shooting occuring whilst currently shooting with this boolean flag
    shooting = false;
    //create a player_collider, assign the newly created ball to a var called pFab
    var pFab : Rigidbody = Instantiate(player_collider, spawnPos.position, spawnPos.rotation);

    //find the forward direction of the object assigned to the spawnPos variable
    var fwd : Vector3 = spawnPos.forward;
    pFab.AddForce(fwd * power * ShootPower);

    //pause before resetting everything
    yield WaitForSeconds(4);

    }

  • Cool nice one buddy , do you mind making all your scripts downloadable like this one ?

  • [...] Shared Unity 3D Video Tutorials | 100% Free | Worms Style Power Bar | The Best Way to Learn Video Game Deve…. [...]

  • nice tutorial , ty !!!
    but for Info your example site http://www.pasteit4me.com/2050001 is down.

  • I wanted to use this worm style power bar in the 3rd challenge.

    • feel free!

    Link for download your sourcecode is down.

  • can you upload a new one example?

    • see latest blog post..

    Great Tutorials!
    i use everything, i learned here and create a minigame in which i shoot spheres around in a room with destroyable objects. my problem is, if i shoot at em with high force, the sphere goes often through the object. is there a simple way to fix that?

    • Its a common problem. Ideally you’ll freeze rotation of the bullet object on the rigidbody, and also try different settings for Interpolate / Extrapolate on the Rigidbody to see if it makes a difference. The other solution is to raycast ahead of the moving object to see if you ‘will’ hit something, then make it react as if you have once the ray is reduced to a certain length.

    thanks for the fast answer!
    i tried different things so far, changing the fixed timestep and changing the collision detection. thicker walls worked fine, but i got thinner objects too…
    i tried the raycast thing, how i do a raycast i know from your tutorial, but i dont have any idea how to get the reaction. changing direction and so on…

  • Just stumbled upon this site and was ecstatic to find this script and such an in depth explanation. Keep up the good work!

  • Amazing, Thats probably how they made the plasma pistol on Halo.

  • My project is not working as intended, I everything is working but the ball that needs to shoot, the particles show, the light shows but not the ball, I linked everything to drag and drop and I have the same names on everything, but when I press space bar, the ball doesnt initializes or anything, any ideas?

  • Would you please reupload the script please?
    Im not someone who is going to post my errors, because i think thats to hard to fix.

    !Please reply via my email!

  • Hey can u give me a muzzle flash script?

  • Hi Will,
    I’ve learned through your basic tutorial. I find them truly awesome and want to call you as my master. I’m currently studying this tut. However, base on this tut, could you guide us how to animate the cannon model such as: rotate its wheel when transforming position or move the gun only up and down. So this will help me understand how to animate gestures of a character imported from 3dsmax.
    Thanks!

  • Cool script and a detailed tutorial. I liked it :)

  • This is pretty cool! should I just copy and paste it?

  • Hello,
    This is a great example!! Can you please let me know where can we find the materials such as Cannon, prefabs etc for the project?

  • Could anyone refresh link ??

  • So, how can I convert the style in Touch Input Button?
    Can anyone help me?
    I’m developing Android .. So I want it to be in button that when I
    click a button the ball will throw

Leave a Comment