- Home /
Enable a component after a specific amount of time?
So im trying to make this PT esq, horror game, and theres something that im having trouble with.
So you know in PT where the introduction was of the character waking up?
Now ive managed to replicate that intro by simply using the animator and adding an animation to the First Person Controller camera. So the animation starts when the game starts right? so ive disabled both the Mouse look and character motor scripts, so I wont be able to walk whilst the animation is playing as the scene starts.
So the animation itself is 16 seconds long, now this is where im needing help, after the animation is done, how do I again enable those two scripts so I could now control the character back?
In general, how can you enable a component after a specific amount of time?
How do you do that in C#? is there an API for it? what are your thoughts?
Answer by bolkay · May 17, 2018 at 05:44 AM
void Start(){
StartCoroutine(Function());
}
IEnumerator Function(){
yield return new WaitForSeconds(seconds);
this.GetComponent<Name>.enabled=true;
}
allright, now were talking. I was always confused about what the startcaroutine function was before along with the waitforseconds code which I never really figured out how to make it work. but now I see it, thanks for this one bro.
Answer by shadowpuppet · May 17, 2018 at 06:10 AM
You are basically starting your game with a cinematic. I would treat it as such and have it entirely separate camera ( sorry, I don't know what PT is) and after 16 seconds disable that camera and enable the FPS controller. I have that a few times in my game where the player hits a certain spot ( trigger zone) and disable EVERYTHING player orientated and show a cinematic camera of the door he just opened . So I would approach it like that. The script above from bolkay, sort of, on the cinematic camera...then after 16 seconds enable player stuff and dis able the start camera
Now that right there also works, and I think this would work more on a Third Person Character controller as using 2 cameras on an FPS seems verbose in my opinion. IEnumerator and Startcaroutine was what made $$anonymous$$e work now.
but still, thanks, I think I could use this method on a 3rd person character next time.
Answer by Shameness · May 17, 2018 at 08:01 AM
See here first:https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html . Add animation event so you can call a public function on the gameobject that animation is attached. First have a script ( you already have) then add public function. Next add animation event, on inspector you can select function for animation event. Lastly write a function does what you want like this:
public void EnableAgain()
{
gameObject.SetActive(true);
}
Your answer
Follow this Question
Related Questions
Confining Mouse Look on X axis 0 Answers
Character refuses to fly 2 Answers
How to disable MouseLook in ingame pause. 1 Answer
Clamping Camera doesn't work 1 Answer
How to apply a deadzone to camera for mouse position on Unity's default FPC? 0 Answers