- Home /
Increase force/size the longer input has been held down.
So I've got a character who on left click, instantiates and fires a bullet from his gun at a constant force using rigidbody.addforce but now I want to add it so the longer you hold the left click down, it will increase in size and force (with a capped limit) until then when you release it fires the projectile.
It's sort of like Samus' bullet in super smash Bros. The longer you hold down it charges more. Thanks all answers appreciated.
Answer by NerdHerdGames · Jan 30, 2018 at 04:22 AM
Just add a couple floats to the stop of your class that handles shooting/creating the bullet. Maybe add a variable to the bullet class if it's a networked object and you set the force inside the bullet objects script(s).
public float BaseForce;
public float MaxForce;
public float ForceIncRate;
private float CurrentForce;
private bool Charging;
void Update() {
if(input.GetButton("Fire1")) {
if(!Charging) {
CurrentForce = BaseForce;
Charging = true;
}
else {
CurrentForce += ForceIncRate * Time.deltaTime;
}
}
else if(!Input.GetButton("Fire1") && Charging) {
Fire(CurrentForce);
Charging = false;
}
}
public void Fire(float force) {
/// create bullet with force here
}
Answer by wesleyjamessihs · Jan 30, 2018 at 05:00 AM
Could you maybe expand on line 24 and below, how would i go about doing creating the bullet. i understand the rest @NerdHerdGames
That kind of depends on how you have your game setup. If your bullet(s) have their own script attached to them, which they probably should, maybe add a method to that script like..
public void SetBulletVelocity(float x) {
gameObject.getComponent<RigidBody>().Velocity = Transform.Forward * x;
}
So in line 24, Instantiate your bullet and then call that method on it.
If you're making a multiplayer game, you could probably use a sync var ins$$anonymous$$d and set the velocity on start/awake
I see that., i want my player to have the script that controls the bullet but what I'm saying is, to increase the force ans size over time I need to multiply the bullets force or size by something and i dont know exactly how to do that, so what would i put when for line 25 (i.e. i need to instantiate it at a position with a set force and multiply thatforce by something) same goes for size. How would i do that i am not advanced with code so i need to know how to.
Your answer
![](https://koobas.hobune.stream/wayback/20220612151601im_/https://answers.unity.com/themes/thub/images/avi.jpg)