- Home /
How do you add a power gauge to a shooting script.
Hi I've got a shooting script (below) attached to the camera. It fires a ball prefab but what I want it to do is when I hold the shooting button(left mouse button) down, it charges the shot so when I release the mouse button it fires the ball prefab with greater power. Here's the shooting script. using UnityEngine; using System.Collections;
public class Shooter : MonoBehaviour {
 public Rigidbody bullet;  
 public float power = 1500f;  
 public float moveSpeed = 2f; 
 void Update (){
     if(Input.GetButtonUp("Fire1")){       
 
     Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;     
     Vector3 fwd = transform.TransformDirection(Vector3.forward);   
     instance.AddForce(fwd * power);
     }
  }
}
               Comment
              
 
               
              Answer by Cherno · Jul 20, 2016 at 11:59 PM
public float maxPower; public float powerUpSpeed = 20;
 if(Input.GetButton("Fire1")){    
      if(power < maxPower) {
               power += Time.deltaTime * powerUpSpeed;
      }
 }
In your GetButtonUp function, which releases the ball, you set the power to zero again.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                