2023-04-29

Issue spawning player

I have a door system in my game that allows the player to switch scenes. Depending on the door he triggers, he is suppose to be placed in different spots on the map.

All my doors have a box collider and a sceneSwitcher script attached to them that looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;

public class SceneSwitcher : MonoBehaviour
{
    float distance;
    public GameObject player;
    public GameObject interactUI;
    public Animator Transition;
    public string levelToLoadNext;
    public float newSpawPointx;
    public float newSpawPointy;
    public float newSpawPointz;

    void OnMouseOver()
    {
        distance = Vector3.Distance(player.transform.position, this.transform.position);
        if (distance <= 5.5f)
        {
            interactUI.SetActive(true);
            gameStatsManager.levelToLoad = levelToLoadNext;
            gameStatsManager.spawnPoint = new Vector3 (newSpawPointx, newSpawPointy, newSpawPointz);
            if (Input.GetMouseButtonDown(0))
            {
                StartCoroutine(LoadLevel());
            }
        }
        else
        {
            interactUI.SetActive(false);
        }
    }
    void OnMouseExit()
    {
        interactUI.SetActive(false);
    }

    void doorAnimationScene()
    {
        SceneManager.LoadScene("doorAnimationScene");
    }

    IEnumerator LoadLevel()
    {
        Transition.SetTrigger("Start");
        yield return new WaitForSeconds(1);
        doorAnimationScene();

    }
}

While the door animation is being player this little script is being called:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class loadScene : MonoBehaviour
{

    public void loadCorrectLevel()
    {
        SceneManager.LoadScene(gameStatsManager.levelToLoad);
        Debug.Log(gameStatsManager.spawnPoint);
    }
}

That just tells the sceneManager what scene to load next.

The gameStatsManager script that is being called in the sceneSwitcher script is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class gameStatsManager : MonoBehaviour
{
    public static string levelToLoad;
    public static Vector3 spawnPoint = new Vector3(1381.94f, 60.86f, 859.23f);
}

It is not in the scenes, it is just a script that holds 2 static variables, levelToLoad which tells the game what scene to load next and spawnPoint which tells the game the vector3 coordinates where the player should be spawned.

When the player places the mouse over the door the levelToLoad and spawnPoint variables are updated automatically.

Each scene has a playerSpawnPointSet empty object with the following script attached to it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerSpawnPointSet : MonoBehaviour
{
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindWithTag("Player");
        player.transform.position = gameStatsManager.spawnPoint;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

The script takes the gameStatsManager.spawnPoint data and assigns it to the player's position.

Now my issue is that for some reason, sometimes the player's position doesn't get updated and when the scene is loaded he stays in his original position. My Debug.Log in the loadScene script is always correct (it is always the vector3 coordinates where the player should be placed) but for some reason Unity doesn't always place the player there...

It seems to mostly happen when I first load the project. Any idea why my system is not always working like it should?



No comments:

Post a Comment