Source Code for Key.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Key : MonoBehaviour{
private SpringJoint2D spring;
// Start is called before the first frame update
void Start(){
spring = GetComponent<SpringJoint2D>();
GameObject backpack = GameObject.FindWithTag("Backpack");
spring.connectedBody = backpack.GetComponent<Rigidbody2D>();
spring.enabled = false;
}
private void OnTriggerEnter2D(Collider2D collision) {
if(collision.gameObject.tag == "Player" && !spring.enabled){
spring.enabled = true;
}
}
}
Source Code for Door.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour{
public GameObject connectedDoor;
public bool teleported = false;
private GameObject player;
// Start is called before the first frame update
void Start(){
player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update(){
if(teleported && Input.GetAxisRaw("Vertical") < 1){
teleported = false;
}
}
private void OnTriggerStay2D(Collider2D collision) {
if(collision.gameObject.tag == "Player" && Input.GetAxisRaw("Vertical") == 1) {
if(Input.GetAxisRaw("Vertical") == 1 && !teleported){
player.transform.position = connectedDoor.transform.position;
connectedDoor.GetComponent<Door>().teleported = true;
}
}
}
}