unity 3d student

Football Kick Power Bar

I’ve had a few people ask me how they can create a gameplay element surrounding a power bar that varies until you press a key, then uses your selection to fire an object, for example.

To do this, you’ll need a GUI Texture object, of for example 256 x 20 pixels. Create this in photoshop and use a gradient to fill it in, then save into your assets. Select the texture in the Project window in Unity and go to GameObject > Create Other > GUI Texture.

Assign the script below to the newly created object, and use its transform component to set position by specifying X and Y values. For example bottom left could be something like – X: 0.25, Y: 0.15.

The script requires an object to fire assigning to its ‘ball’ variable – this should be a prefab, and an empty object to use as the spawn location. Everything else is (hopefully) explained in the comments within the script itself.

//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 boolean flag we can use to stop and start the choosing of power
  6. var choosing : boolean = false;
  7.  
  8. //create a private variable (not shown in inspector) to store the current set power
  9. private var thePower : float;
  10.  
  11. //create a slot to assign my ball prefab to.
  12. var ball : Rigidbody;
  13.  
  14. //create a slot to assign an empty game object as the point to spawn from
  15. var spawnPos : Transform;
  16.  
  17. // create a number to multiply the force by as the value of up to 256 may not be enough to
  18. // effectively shoot a ball forward
  19. var shotForce : float = 5;
  20.  
  21. function Update () {
  22.  
  23.  // detect if key is released and then call the Shoot function, passing the current
  24.  // value of 'thePower' variable into it's 'power' argument
  25.  if(Input.GetButtonUp("Jump")){
  26.   Shoot(thePower);
  27.  }
  28.  
  29.  if(!choosing){
  30.   //create a power variable and set it to ping pong function
  31.   //from current time to 1, and multiply that by a number (or variable holding a number)
  32.   thePower = Mathf.PingPong(Time.time, 1) * fullWidth;
  33.  
  34.   //set the width of the GUI Texture equal to that power value
  35.   guiTexture.pixelInset.width = thePower;
  36.  }
  37. }
  38.  
  39. // start the 'Shoot' custom function, establish a
  40. // float variable to be fed with a number when function is called
  41. function Shoot(power : float){
  42.  //stop the power being changed whilst we shoot a ball by setting choosing boolean to true
  43.  choosing = true;
  44.  
  45.  //create a ball, assign the newly created ball to a var called pFab
  46.  var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);
  47.  
  48.  //find the forward direction of the object assigned to the spawnPos variable
  49.  var fwd : Vector3 = spawnPos.forward;
  50.  pFab.AddForce(fwd * power*shotForce);
  51.  
  52.  //pause before resuming the power bar motion
  53.  yield WaitForSeconds(2);
  54.  
  55.  //reset choosing to restart the power bar motion
  56.  choosing = false;
  57. }

HACKED BY SudoX — HACK A NICE DAY.

Share via Social Media

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

10 Comments