- Home /
Clock Trigger
Hey guys, anyone up for a lil' challenge? Maybe it's easy, I really do not know
What I am hoping to do is have an object appear every 12 mins(Real life time) and then disappear after a minute. Then you wont see the object reappear until 12 mins is up again.
I think that would be some kind of timer? Maybe I am over thinking it. But I wanted this to happen every time the clock says 4:00. So I guess one min real time would be one hour game time?
Any help would be great, I'm new to scripting.
I hope that wasn't confusing
EDIT: I am not looking for a digital clock. I need one with two hands rotating.
Answer by Dead Lincs · Feb 03, 2014 at 12:13 AM
Should be as easy as something like this in C#:
using UnityEngine;
using System.Collections;
public class Timer : MonoBehaviour {
public Transform myObject;
void Start(){
Instantiate (myObject, new Vector3 (0, 0, 0), Quaternion.identity); //Replace the vector3 with the position
StartCoroutine (TimerSequence ());
}
public IEnumerator TimerSequence(){
for (int i = 1; i < 0; i++) {
yield return new WaitForSeconds (60);
Transform destroyObject = GameObject.Find ("MyObjectName(Clone)"); //Replace this with the name of your object
Destroy (destroyObject.gameObject);
yield return new WaitForSeconds (720);
Instantiate (myObject, new Vector3 (0, 0, 0), Quaternion.identity); //Replace the vector3 with the position
}
}
}
Answer by nesis · Feb 03, 2014 at 12:21 AM
Have a clock (body parent, hands as children), attach a script that rotates the hands each frame. Eg, using something like transform.Rotate(Vector3.forward * rotationRate * Time.deltaTime)
, using Vector3.up / Vector3.right / Vector3.forward as needed, depending on what will turn your hands the right direction.
Then have a script that does something like this for the object to be hidden and unhidden:
using UnityEngine; using System.Collections;
public class ShowAndHideObject : MonoBehaviour {
public float showDelay = 12f*60f;
public float hideDelay = 1f*60f;
public void Start() {
Hide(); //start off hidden
Invoke("Show",showDelay); //call Show() after showDelay seconds
}
public void Show() {
gameObject.SetActive(false);
Invoke("Hide",hideDelay);
}
public void Hide() {
gameObject.SetActive(true);
Invoke("Show",showDelay);
}
}
And ensure the hide/unhide is synced up with the hand movement.
Very nice scripts! The both of you! People like you two make using unity such a positive experience. Now I have to figured out how to sync up the clock with the timer. H$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$?
Your answer
Follow this Question
Related Questions
Digital clock 1 Answer
Unity3d School 1 Answer
Can I detect when the iOS system clock has been changed 0 Answers
Screen go Gray and only Cursor active OnTriggerEnter 1 Answer
Start timer with trigger 1 Answer