- Home /
Pause gameobjects with tag?
I'm currently working on a simple cutscene script for my game and it's all working very well, with the exception that I'm not sure how to go about pausing all objects that aren't involved. Basically, what I want to do is to prevent players from moving around and enemies from engaging the player.
Any clues?
Answer by betaFlux · Dec 31, 2018 at 12:09 AM
For instance: In your ai script in the Update function you could check a bool if the rest is allowed to be executed:
public bool run;
Update()
{
if(run == false)
return;
// Logic to block
}
Then in your cutscene manager you could get all objects by tag or component and toggle the 'run' variable as needed:
public bool run;
void SetCutSceneStuff()
{
GameObject[] npcs = GameObject.FindGameObjectsWithTag("NPC");
foreach(GameObject npc in npcs)
{
npc.GetComponent<NPCController>().run = false; // Or true
}
}
EDIT: I remember I did the same some years ago. I let my cutscenes run on a custom deltaTime.
Thanks to @tehpro_unity's link you should be able to integrate pausing. Use that and create your own deltaTime somewhere in a GameMaster script like this:
private static float prevRealTime;
private float thisRealTime;
void Update() {
prevRealTime = thisRealTime;
thisRealTime = Time.realtimeSinceStartup;
}
public static float cutsceneDeltaTime {
get {
if(Time.timeScale > 0f)
return Time.deltaTime / Time.timeScale;
return Time.realtimeSinceStartup - prevRealTime;
}
}
Now you can use "GameMaster.cutsceneDeltaTime" for everything that you want to move in your cutscenes. It should work even if Time.timeScale = 0.000000001f (game is paused).
Answer by tehpro_unity · Dec 31, 2018 at 12:13 AM
I think this answer will help you: https://answers.unity.com/questions/7544/how-do-i-pause-my-game.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612184817im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Freeze game using timer from start, then resume play when click the button. It is possible ? 0 Answers
How do I change the value of a tag with raycasting? 0 Answers
Tags not working? 1 Answer
Detecting tags on an animated gameobject 0 Answers
How to make my player object increase its speed as it moves. 1 Answer