- Home /
three second timer.
Hi everyone, I am creating a 3d flying camera game, where the camera flies around on all three axises, and I am trying to figure out how to get my "Overdrive" feature, that goes at 100f, to work Here's my current code. I don't currently have a timer feature set yet so yeah. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class move : MonoBehaviour { private bool stop = false; public float smooth = 5.0f; public float tiltAngle = 60f; public float moveSpeed = 2f;
void Update()
{
if (stop == false)
{
Vector3 movement = transform.rotation * Vector3.forward * moveSpeed * Time.deltaTime;
transform.position += movement;
}
float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, 0);
transform.rotation *= target;
if (Input.GetKeyDown(KeyCode.P))
{
stop = !stop;
}
if (Input.GetKeyDown(KeyCode.Space) && moveSpeed == 2f)
{
moveSpeed = 20f;
}
else if (Input.GetKeyDown(KeyCode.Space) && moveSpeed == 20f)
{
moveSpeed = 2f;
}
if (Input.GetKeyDown(KeyCode.O) && Input.GetKeyDown(KeyCode.Z) && moveSpeed == 20f)
{
Debug.Log ("overdrive enabled")
moveSpeed = 100f;
}
else if (Input.GetKeyDown(KeyCode.O) && Input.GetKeyDown(KeyCode.Z) && moveSpeed == 100f)
{
Debug.Log ("overdrive disabled")
moveSpeed = 2f;
}
}
}
What am I doing wrong? Is there something that I missed when adding this? Also how to attach a timer to it is necesarry.
Answer by K-Anator · May 06, 2019 at 08:51 PM
If I've learnt one thing in the past month of working in Unity, it's that co-routines are good for timers. I don't fully understand them per say, but I've managed to do a few things with various iterations of this script:
[SerializeField]
private float startTimer;
private GameObject player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
StartCoroutine("CountDown");
}
IEnumerator CountDown()
{
yield return new WaitForSeconds(startTimer);
player.GetComponent<MovementController>().AllowMovement();
}
something like this should be close to what you need:
[SerializeField] private float OverdriveTime;
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
StartCoroutine("OverDrive"); //Start dat co-routine
}
IEnumerator OverDrive()
{
moveSpeed = 200f; //Become a fast boi
yield return new WaitForSeconds(OverdriveTime); //Wait the amount of time you want
moveSpeed = 2f; //Become a slow boi
}
Once you press space then simultaneously press O and Z overdrive is enabled but the code right after that immediately disables it because it is input.getkeydown() O and Z.
I would agree with that if there wasn't the else statement in there. isn't that supposed to wait until you take your fingers off the keys to actually have that working? or how should I fix that?
also, I just changed the code so that it is only the Z key. but all I really need is fro the timer to be attached.
so now everything is working as it should but there are three errors. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class move : $$anonymous$$onoBehaviour { private bool stop = false; public float smooth = 5.0f; public float tiltAngle = 30f; public float moveSpeed = 2f; public float OverdriveRun = 3f; private bool OverdriveRunning = false; private bool CanRun = true; public float Cooling = 15f; private bool OverdriveCooldown = false;
void Start()
{
gameObject.GetComponent<ParticleSystem>().enableEmission = false;
}
void Update()
{
if (stop = false)
{
tiltAngle = 30f;
Vector3 movement = transform.rotation * Vector3.forward * moveSpeed * Time.deltaTime;
transform.position += movement;
}
else
{
tiltAngle = 10f;
}
float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, 0);
transform.rotation *= target;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))
{
stop = !stop;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && moveSpeed < 20f)
{
Debug.Log ("faster");
moveSpeed = 20f;
}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && moveSpeed >= 20f)
{
Debug.Log ("slower");
moveSpeed = 2f;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.O) && moveSpeed >= 20f && moveSpeed < 100f && CanRun = true)
{
Debug.Log ("evenfaster");
moveSpeed = 100f;
OverdriveRunning = true;
}
if (OverdriveRunning = true)
{
OverdriveRun - Time.deltaTime;
}
if (OverdriveRun <= 0)
{
OverdriveRunning = false;
moveSpeed = 2f;
OverdriveCooldown = true;
}
if (OverdriveCooldown = true)
{
Cooling - Time.deltaTime;
}
if (Cooling <= 0)
{
CanRun = true;
OverdriveRun = 3f;
OverdriveCooldown = false;
}
if (OverdriveCooldown = false)
{
Cooling = 15f;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && moveSpeed >=2f)
{
moveSpeed = 30f;
gameObject.GetComponent<ParticleSystem>().enableEmission = true;
}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && moveSpeed >= 30f && moveSpeed < 31f)
{
moveSpeed = 2;
gameObject.GetComponent<ParticleSystem>().enableEmission = false;
}
}
}
note: there is a particle system attached, and there is a cool down time attached as well as the three seconds of Overdrive.