I would like to add a delay to activating a camera
This is the entire script, I just want the camera to activate 0.5 seconds after you press the mouse button. How would I go about this? Note: I know I could make this much more compact but I like it like this. C# please
using UnityEngine; using System.Collections;
public class Aim_s : MonoBehaviour {
public bool Aim = false;
public GameObject Cam;
float delay = 1;
void Start() { Cam.active = false; }
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Aim = true;
if (Aim == true)
{
Cam.active = true;
}
}
if (Input.GetMouseButtonUp(1))
{
Aim = true;
if (Aim)
{
Cam.active = false;
}
}
}
}
Answer by SniperEvan · Oct 24, 2015 at 02:30 AM
float timer= float.MaxValue;
When the user presses the mouse timer = Time.time + .5f (set the timer .5 seconds in the future)
in Update(){ if(Time.time > timer) activate camera }
When deactivating: timer = float.MaxValue
Let me know if you want more details. Basically we have a timer and when we click the timer gets set for .5 seconds. Then when Time.time passes the timer we activate the camera.
Answer by Ximerous · Oct 24, 2015 at 03:59 AM
Thanks, I ended up with this, I finished getting rid of extras as well! Let me know if there is anything I did wrong or could do better.
using UnityEngine; using System.Collections;
public class Aim_s : MonoBehaviour {
public bool Aim = false;
public GameObject Cam;
public float timer = float.MaxValue;
void Start() { Cam.active = false; }
void Update()
{
if (Input.GetMouseButtonDown(1))
{
timer = Time.time + .8f;
}
if (Time.time > timer)
{
Cam.active = true;
}
if (Input.GetMouseButtonUp(1))
{
timer = float.MaxValue;
}
if (Time.time <= timer)
{
Cam.active = false;
}
}
}
Awesome! Your code looks good. If you wanted to, ins$$anonymous$$d of comparing time.time with timer twice you could use a simple if-else statement. That would make the code a little bit easier to read for future Ximerous.
Your answer
Follow this Question
Related Questions
Wait time Before Camera Switching? 0 Answers
Mining in RTS 0 Answers
How to make an object go the direction it is facing? 0 Answers