Hello,
I am a bit of a unity noob and was wondering how I could make an explosion where the mouse is with force dependent on how long the user clicks. Can’t find any sources explaining how to do this.
I would be grateful for any answers.
Thx.
First you need to setup an explosion which gets strength as an parameter. Then use this code:
public class ChargingExplosionStrength : MonoBehaviour {
private float strength = 0.0f;
public float maxSeconds = 1.0f;
public float chargeSpeed = 1.0f;
public void Update() {
if(Input.GetMouseButton(0)) {
this.strength += Time.deltaTime * this.chargeSpeed;
}
if(this.charged >= this.maxSeconds) {
// pass your explosion strength here and do the explosion
this.strength = 0.0f
}
}
}
As long as the LMB is clicked the strength will increase. The default charge speed is 1 unit per second and after 1 second it goes off automatically. You can also increase the maxSeconds to make the player charge longer or chargeSpeed to increase the charge faster (or both to meake a stronger explosion in the same amount of time).