How to make explosion which is stronger when you click longer?
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.
Answer by Zodiarc · Oct 26, 2016 at 11:52 AM
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).
Thx so much, making a game where you use explosions to send a Yak flying through space to get to a door. Really important mechanic. Thx again. Really useful.
Your answer
Follow this Question
Related Questions
3D vs 2D build size 0 Answers
How can I create this particle effect 0 Answers
I need to bend a stick and am lost 1 Answer