Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to keep doors always open.

I can make my player get the key and open the door and get through the door the first time, but when my player respawns the player can no longer get through the door. Here is my code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Door : MonoBehaviour

{

    private PlayerMovement thePlayer;

    

    public GameObject connectedDoor;

    public SpriteRenderer theSR;

    public Sprite doorOpenSprite;


    public bool doorOpen, waitingToOpen;


    // Start is called before the first frame update

    void Start()

    {

        thePlayer = FindObjectOfType<PlayerMovement>();

    }


    // Update is called once per frame

    void Update()

    {

        if(waitingToOpen)

        {

            if(Vector3.Distance(thePlayer.followingKey.transform.position, transform.position) <0.1f)

            {

                waitingToOpen = false;


                doorOpen = true;


                theSR.sprite = doorOpenSprite;


                thePlayer.followingKey.gameObject.SetActive(false);

                thePlayer.followingKey = null;

            }

        }


        if(doorOpen && Vector3.Distance(thePlayer.transform.position, transform.position) < 1f && Input.GetAxis("Vertical") > 0.1f)

        {

            thePlayer.transform.position = connectedDoor.transform.position;

        }

    }


    private void OnTriggerEnter2D(Collider2D other)

    {

        if(other.tag == "Player")

        {

            if(thePlayer.followingKey != null)

            {

                thePlayer.followingKey.followTarget = transform;

                waitingToOpen = true;

                

            }

        }

    }

}

Answers

  • Use playerprefs try assigning some string if the player opens a door and also when the lvl starts just check that what the playerprefs says if it says open than make the door open so the next time player respawn the door wilp be open

Sign In or Register to comment.