I have a charge shot weapon, you hold down a button, a float counts up, when it reaches a certain number and the player releases the button the shots are delivered via invoke repeat until the counter goes back down to 0.
The problem i’m having is the charge up is happening much faster than the charge down. The charge up is inside Update() as it needs to keep track of when the player is pressing the button, and because I have used invoke repeat I have a separate function outside of update that is actually firing the weapon.
Is there a simple way to force the count up and down times to run at the same speed?
You should try using the FixedUpdate method rather than the Update method. While Update is called every frame, FixedUpdate is called every fixed framerate frame. This might solve your issue. Another way of doing that and “forcing” the same speed for the charging, you could just loop your charging up and down in a for-loop.
FixedUpdate is for stuff that needs to be synced with the physics loop. It’s not designed for constant speed stuff. Better to properly use deltaTime.
Plus it won’t solve the problem of charging and discharging rate being different.
A better idea would be to implement this as a simple FSM. The gun must discharge every frame while its firing. You can do it all in Update with a couple of bools to keep track of state. Or you could use a coroutine.
Sorry for the delay, I haven’t been able to work on my game due to other things getting in the way.
I see, well the charge down time is about right, but the charge up is too fast, how can I force the charge up to be 0.05f as well? I tried using Invoke but it says it cant convert float to string.
I have tried using invoke but nothing is happening when I press the button, I cant understand why as I am using it exactly the same way as I am for firing the weapon:
[SerializeField]
float chargeRate = 0.5f;
[SerializeField]
float disChargeRate = 0.1f;
[SerializeField]
float currentCharge;
[SerializeField]
float maxCharge = 10f;
// Use this for initialization
void Start () {
InvokeRepeating("ChargeUp", 0, chargeRate);
}
void ChargeUp()
{
if (Input.GetMouseButton(0))
{
currentCharge += chargeRate;
CancelInvoke("DisCharge");
}
if (currentCharge >= maxCharge)
{
currentCharge = 10f;
Debug.Log("Max Charge");
}
}
void DisCharge()
{
currentCharge -= disChargeRate;
if (currentCharge <= 0)
{
currentCharge = 0;
}
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0))
{
InvokeRepeating("DisCharge", 0, disChargeRate);
}
}
basicly we invoke the function Chargeup with a delayed start of 0 and a delay of 0.5f the chargeup rate. and as soon as we lift our finger from the button it discharges with the rate i have given it aka 0.1f. And as soon as we hold the mousebutton down again we cancle Discharge and once again charge up.
the reason why ChargeUp is faster for you is because i assume u hold down the Fire Button and that triggers the same invokeRepeating multiple times or as many frames as u are holding the button down
the way you need to do it is either subscribe like i did in start with invoke repeating or do it another way.
also for some reason i never found CancelInvoke(); to work but CancelInvoke(“Name of your Method”); < that works for me and also it gives me better control over what i want to stop because with just CancelInvoke(); your actually saying all invokes that this specific object has done has to be stopped
Thanks a lot for that, putting the Invoke in Start and changing the CancelInvoke command fixed my issues. The only problem I now have regards the particle effect + sound that play when the charging up occurs. Currently it is set up like this:
The problem is if you spam press the button the sound and charge particle will continue to reset. Is there a way to Invoke these effects then cancel the invoke if the player lifts off the button? I tried using a similar Invoke command as I used for the actually charging but the effects wont play.