Im trying to create a crash bandicoot spin but how do i make it disabled after a second?
Im making a crash bandicoot game and im currently working on the spin mechanic and the spin works when the key is pressed but the renderer wont disable after the spin so it just keeps spinning! how do i get it to work? This is my script at the moment? Please help!
Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpinStopper : MonoBehaviour {
public double timer = 1;
public GameObject SpinCB;
public GameObject RealCB;
void Update()
{
timer += Time.deltaTime;
if(timer <= 0)
{
timer -= 0.5;
SpinCB.SetActive(false);
RealCB.SetActive(true);
}
}
}
Where is your code that means to stop the script? And what do you intend to do with above-posted SpinStopper code? It looks almost as if your timer gets decreased infinitely, because you're saying "If it's equal-below zero, keep subtracting from it".
Im really new to scripting so im not that experienced! But what should i do to make it work?
Answer by Archelous · Feb 15, 2020 at 02:13 PM
How to count down is just subtract the time each frame took from the duration. Something like this.
public float timer= 1;
public GameObject SpinCB;
public GameObject RealCB;
void Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
SpinCB.SetActive(false);
RealCB.SetActive(true);
}
}
reset your objects and timer to reset the situation
I would put your timer/action in a bool (like a spin button). when the bool is triggered (spin button hit) you want to set your spin to active and set your timer to 1. when your if statement kicks it will turn back to real and set your bool back to false and wait for the spin button to be hit again.
put a bool check on your spin button so that if your in a spin already you dont start a spin
public float timer = 1;
public GameObject SpinCB;
public GameObject RealCB;
public bool spinning;
void Update()
{
if (spinning)
{
timer -= Time.deltaTime;
//stop spinning when done
if(timer <= 0)
{
SpinCB.SetActive(false);
RealCB.SetActive(true);
spinning = false;
}
}
}
void SpinButton()
{
//make sure you arent spinning already
if (!spinning)
{
//set up your spin
timer = 1;
SpinCB.SetActive(true);
RealCB.SetActive(false);
spinning = true;
}
}
I still have a problem i dont know how to fix! As soon as i press play it starts to count down the spin timer then it goes all the way to -0.001279993 and stops there then nothing happens from that point forward? I want to be able to spin once everytime i press down "q" for a certain amount of time then it all resets and the spin model toggles off and the player model toggles back on with no problem? how do i fix that @steven1022 ? Please help me with this im so close to getting the mechanics finished!
Answer by steven1022 · Feb 15, 2020 at 06:17 PM
This should be in your Update
if (Input.GetKeyDown(KeyCode.Q))
{
SpinButton();
}
I Finally got it to work thank you so much for the help!
Your answer
Follow this Question
Related Questions
Slider Fill Area deactivate after jump 2 Answers
Selectively enable and disable Gamepads?,D 0 Answers
Disable player movement for some seconds? 0 Answers
How to stop a countdown timer and disable/stop everything in the scene? 1 Answer
Buttons: How to enable, disable and enable same button again? 1 Answer