Opening Door pressing a key (keyboard or pad)
Hi everyone, I have a problem with the code I wrote. According to the logic of the code, the player should open the door when he presses the right key, however when the player presses the key to open the door, the other doors open also. How can I make sure that only the door concerned is opened?
public class Door : MonoBehaviour
{
private bool doorOpen = false;
private Animator animator;
public Canvas canvas;
// Start is called before the first frame update
void Awake()
{
animator = GetComponent<Animator>();
canvas.gameObject.GetComponent<Canvas>();
if (canvas.gameObject.activeSelf == true)
{
canvas.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if(canvas.gameObject.activeSelf == true && Input.GetAxis("PickUp") != 0)
{
StartCoroutine("ChangeDoorState");
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
canvas.gameObject.SetActive(true);
Debug.Log("Sta toccando");
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
canvas.gameObject.SetActive(false);
Debug.Log("Non Sta toccando");
}
}
public void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "Player" && doorOpen == true)
{
canvas.gameObject.SetActive(false);
}
if (other.gameObject.tag == "Player" && doorOpen == false)
{
canvas.gameObject.SetActive(true);
}
}
public IEnumerator ChangeDoorState()
{
if(doorOpen == false)
{
animator.SetBool("open", true);
doorOpen = true;
yield return new WaitForSeconds(5);
animator.SetBool("open", false);
}
}
}
Answer by Argo2007 · May 25, 2019 at 03:17 PM
With this code I solved the fact that all the doors were opened together. Now, when I go to open a second door, it opens without pressing keys, the problem is solved by entering or exiting the door collider twice.
public class Door : MonoBehaviour
{
private bool doorOpen = false;
private Animator animator;
public Canvas canvas;
// Start is called before the first frame update
void Awake()
{
animator = GetComponent<Animator>();
canvas.gameObject.GetComponent<Canvas>();
if (canvas.gameObject.activeSelf == true)
{
canvas.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if (canvas.gameObject.activeSelf == true && Input.GetAxis("PickUp") != 0)
{
doorOpen = true;
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (canvas.gameObject.activeSelf == false)
{
canvas.gameObject.SetActive(true);
}
Debug.Log("Sta toccando");
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
canvas.gameObject.SetActive(false);
animator.SetBool("open", false);
doorOpen = false;
Debug.Log("Non Sta toccando");
}
}
public void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Player" && doorOpen == true)
{
animator.SetBool("open", true);
canvas.gameObject.SetActive(false);
}
if (other.gameObject.tag == "Player" && doorOpen == false)
{
canvas.gameObject.SetActive(true);
}
}
}
Your answer
Follow this Question
Related Questions
How to run a script twice in a scene 0 Answers
Move to next animation with clicking the game object 0 Answers
animation scirpting 1 Answer
How can I improve this code? 1 Answer
Call animation only once? 1 Answer