- Home /
Fnaf Rebot, not working with, Door.
I am making a FNAF game and, I have code for doors, but when I run it, I always die, or I always don't die. Can someone please help me the code for the doors, and the code for the animatronic is below!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Doors : MonoBehaviour
{
//Code For The Doors.
public GameObject DoorLeft;
public GameObject DoorRight;
public bool DoorLeftActive = false;
public bool DoorRightActive = false;
// Start is called before the first frame update
void Start()
{
DoorLeft.SetActive(false);
DoorRight.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
if (DoorLeftActive)
{
DoorLeft.SetActive(false);
DoorLeftActive = false;
Movement.IsOnStageOfDeath = false;
Debug.Log("Stage of Death is = 0");
} else
{
DoorLeft.SetActive(true);
DoorLeftActive = true;
Movement.IsOnStageOfDeath = true;
Debug.Log("Stage of Death is = 1");
}
}
if (Input.GetKeyDown(KeyCode.D))
{
if (DoorRightActive)
{
DoorRight.SetActive(false);
DoorRightActive = false;
Movement.IsOnStageOfDeath = false;
}
else
{
DoorRight.SetActive(true);
DoorRightActive = true;
Movement.IsOnStageOfDeath = true;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Movement : MonoBehaviour
{
//Code for the Animatronic.
public GameObject Bonnie;
public GameObject SpawnPoint;
/// //////////////////////////////////
public GameObject BonnieFirstOutput;
public GameObject BonnieSecondOutput;
public GameObject BonnieThirdOutput;
/// ///////////////////////////////////////
public GameObject BonniesFirstPositionOutput;
public GameObject BonnieSecondPositionOutput;
/// ////////////////////////////////////////
public GameObject BonniesSecondToLast01;
public GameObject BonniesSecondToLast02;
float RandomNumberToStart;
public static bool IsOnStageOfDeath = false;
// Start is called before the first frame update
void Start()
{
StartCoroutine(StartFirstPosition());
}
IEnumerator StartFirstPosition()
{
Debug.Log("StartingWaitTime");
int RandomJumpscareStart = Random.Range(30, 100);
yield return new WaitForSeconds(RandomJumpscareStart);
int WhichPoint = Random.Range(1, 4);
if (WhichPoint == 1)
{
Debug.Log("First Point");
Bonnie.transform.position = BonnieFirstOutput.transform.position;
}
if (WhichPoint == 2)
{
Debug.Log("SecondPoint");
Bonnie.transform.position = BonnieSecondOutput.transform.position;
}
if (WhichPoint == 3)
{
Debug.Log("Third Point");
Bonnie.transform.position = BonnieThirdOutput.transform.position;
}
StartCoroutine(StartSecondPosition());
}
IEnumerator StartSecondPosition()
{
Debug.Log("StartingSecondWaitTime");
int RandomJumpscareStart = Random.Range(45, 95);
yield return new WaitForSeconds(RandomJumpscareStart);
int WhichPoint = Random.Range(1, 3);
if (WhichPoint == 1)
{
Debug.Log("First Point");
Bonnie.transform.position = BonniesFirstPositionOutput.transform.position;
}
if (WhichPoint == 2)
{
Debug.Log("SecondPoint");
Bonnie.transform.position = BonnieSecondPositionOutput.transform.position;
}
StartCoroutine(StartThirdPosition());
}
IEnumerator StartThirdPosition()
{
Debug.Log("StartingSecondWaitTime");
int RandomJumpscareStart = Random.Range(20, 50);
yield return new WaitForSeconds(RandomJumpscareStart);
int WhichPoint = Random.Range(1, 3);
if (WhichPoint == 1)
{
Debug.Log("First Point");
Bonnie.transform.position = BonniesSecondToLast01.transform.position;
}
if (WhichPoint == 2)
{
Debug.Log("SecondPoint");
Bonnie.transform.position = BonniesSecondToLast02.transform.position;
}
IsOnStageOfDeath = true;
StartCoroutine(VergeOfDeath());
}
IEnumerator VergeOfDeath ()
{
yield return new WaitForSeconds(8);
if (IsOnStageOfDeath = false)
{
Bonnie.transform.position = SpawnPoint.transform.position;
StartCoroutine(StartFirstPosition());
} else if (IsOnStageOfDeath = true)
{
JumpScare();
}
}
void JumpScare()
{
Debug.Log("You Got Stuffed");
PlayerPrefs.SetString("lastLoadedScene", SceneManager.GetActiveScene().name);
SceneManager.LoadScene("Fail Scene");
}
}
What exactly do you want to happen when you run your code?
When the doors are open you are able to be Jumpscared, and when they are closed you can't be and It moves to spawnpoint, and reruns the corotine.
Answer by sean244 · Feb 02, 2019 at 11:49 PM
Simplify your Door script to just
using UnityEngine;
public class Doors : MonoBehaviour
{
public GameObject DoorLeft;
public GameObject DoorRight;
void Start()
{
DoorLeft.SetActive(false);
DoorRight.SetActive(false);
}
void Update()
{
//All we're doing is toggling the doors. If they're active, we deactivate them; if they're inactive, we activate them
if (Input.GetKeyDown(KeyCode.A))
{
DoorLeft.SetActive(!DoorLeft.activeSelf);
}
else if (Input.GetKeyDown(KeyCode.D))
{
DoorRight.SetActive(!DoorRight.activeSelf);
}
//If either of the doors are active, Movement.IsOnStageOfDeath is true, otherwise it's false.
Movement.IsOnStageOfDeath = (DoorLeft.activeSelf || DoorRight.activeSelf);
}
}
An then in your Movement function, re-write your VergeOfDeath coroutine to
IEnumerator VergeOfDeath()
{
yield return new WaitForSeconds(8);
if (IsOnStageOfDeath == false)
{
Bonnie.transform.position = SpawnPoint.transform.position;
StartCoroutine(StartFirstPosition());
}
else
{
JumpScare();
}
}
For some reason I get killed when the doors are closed and when they are open I can live. So should I switch the inside parts of the corotine?
IEnumerator VergeOfDeath() { yield return new WaitForSeconds(8); if (IsOnStageOfDeath == false) { Bonnie.transform.position = SpawnPoint.transform.position; StartCoroutine(StartFirstPosition()); } else { JumpScare(); } }
Well you shouldn't have switched them, because the logic in your VergeOfDeath is fine. We just need to figure out why the value of IsOnStageOfDeath isn't accurate. You want it so that if either of the doors objects are active, IsOnStageOfDeath should return true and JumpScare should be executed, right?
Yes. That is correct, but it works fine with changing the options.
Your answer
![](https://koobas.hobune.stream/wayback/20220612190934im_/https://answers.unity.com/themes/thub/images/avi.jpg)