This is my first game so bare with me here… I am trying to make a puzzle game where the goal is to get to the next room. I have it set up so when the player presses the space bar when on top of the door, they are placed in the next scene(room). However, when you switch scenes, the player stays at the same position. I am trying to make a game object that when the player switches to the next scene, it teleports the player to the game object. Here is my code for the teleporter:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AreaEnterance : MonoBehaviour
{
public string transitionName;
public GameObject player;
// Start is called before the first frame update
void Start()
{
if(player.GetComponent<PlayerController>().areaTransitionName == transitionName)
{
player.GetComponent<PlayerController>().transform.position = this.transform.position;
}
}
// Update is called once per frame
void Update()
{
}
}
And here is my code for the door:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NextScene : MonoBehaviour
{
public string areaToLoad;
public string areaTransitionName;
public GameObject player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerStay2D(Collider2D other)
{
if(other.tag == "Player" && Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene(areaToLoad);
player.GetComponent<PlayerController>().areaTransitionName = this.areaTransitionName;
}
}
}
I cannot figure out why the code isn’t working… please help!