change timer float while still counting down
I'm working on a simple Multi-Kill script for my game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KillManager : MonoBehaviour
{
[Header("Player Statistics")]
public float kills;
public float prevKills;
public float deaths;
[Header("Multi-Kill Timer")]
public float speed;
public float killTimer;
public float killtimerReset;
public bool killChain;
public float chainKills;
[Header("Multi-Kill Status")]
public bool doubleKill;
public bool tripleKill;
public bool quadKill;
public bool pentaKill;
// Start is called before the first frame update
void Start()
{
prevKills = 0;
}
void multiTimer()
{
if(kills > prevKills)
{
killChain = true;
if (killChain)
{
killTimer -= speed * Time.deltaTime;
}
if(killTimer <= 0)
{
killChain = false;
prevKills = kills;
}
if(chainKills == 2)
{
doubleKill = true;
}
if (chainKills == 3)
{
tripleKill = true;
}
if (chainKills == 4)
{
quadKill = true;
}
if (chainKills == 5)
{
pentaKill = true;
}
}
}
// Update is called once per frame
void Update()
{
multiTimer();
chainKills = kills - prevKills;
if (doubleKill)
{
tripleKill = false;
quadKill = false;
pentaKill = false;
}
if (tripleKill)
{
doubleKill = false;
quadKill = false;
pentaKill = false;
}
if (quadKill)
{
doubleKill = false;
tripleKill = false;
pentaKill = false;
}
if (pentaKill)
{
doubleKill = false;
tripleKill = false;
quadKill = false;
}
}
}
How can I make it so I can reset the timer back to it's default timer 5f without stopping the counting down?
I tried this:
if(chainKills == 2)
{
doubleKill = true;
killTimer = 5;
killTimer -= speed * Time.deltaTime;
}
However that simply hard-codes killTimer to 5 and will not count down like I'd like it. Please share your unity expertise with a aspiring novice!
Answer by xxmariofer · Feb 05, 2019 at 07:50 PM
well thats a really overenginered solution and a bit hard to understand. setting the value to 5 will never stop the countdown, so what is stopping the countdown is something else. first you need to remove the killchain = true inside the multplier method since thats a bug 100%, whats the kills value? it is set in the inspector? is there any extra code you are not sharing? since if kills and prevkills are not being updated and the killchain is always true is not posible that just after setting killtimer = 5; the countdown stops.
Everything in this code is working correctly, the kills comes from my Game$$anonymous$$anager script which isn't attached. The only thing not working in this over complicated mess is how to reset the timer. For example once I get a kill, kill chain is true as long as the kill timer > 0. So if I now get a double kill within the 5 seconds, I'd like the timer to start back at 5 and count down again without resetting the kill chain bool
The reasoning for having the kill chain bool is because when the player gets a kill, killChain is true. While killChain is true, prev$$anonymous$$ills will not be updated until killChain is false, then prev$$anonymous$$ills will equal kills. This is done so i can detect how many kills the player is getting during the combo.
yes, but you are doing this, i promise you you can just delete killChain and will make no difference :)
killChain = true;
if (killChain)
{
killTimer -= speed * Time.deltaTime;
}
comparing kill chain there makes no sense, cause it will alway be true (you are setting it to true just before comparing it), in the exact same point you are setting up your kills value (in the other script), set up your killTimer = 5; and tell me if that didnt work :)
This is my script in action, as you can see the kills do populate and prev$$anonymous$$ills updates after the killChain bool is false.
Answer by lbryan212 · Feb 05, 2019 at 07:46 PM
While visiting the toilet I believe I found the answer to my question.
public float killTimer;
public float maxTimer;
public float diffrenceTimer;
diffrenceTimer = maxTimer(5) - killTimer(whatever it is)
killTimer = killTimer + diffrenceTimer;
Your answer
Follow this Question
Related Questions
Trigger a Timer 2 Answers
Farming game Time management 0 Answers
Increase a value by one every minute 2 Answers
Milliseconds Timer Question 1 Answer
How to slow down my timer script? 0 Answers