Home > database >  Changing Scenes and load in a specific location
Changing Scenes and load in a specific location

Time:07-24

So, I am developing a project right now and I wanted the player to be able to enter a building (changing scenes) and exit spawning in the right conditions (spawn in the right place - the building entrance).

To do so I tried a save system described by Brackeys in the video: SAVE and LOAD systems in Unity. But I was getting a lot of errors and decided to simplify for now the project and just conserve between scenes some sort of variable that stored the correct position to save the player location. For that, I checked the video https://youtu.be/wNl--exin90. I implemented it and it works half the time.

Here is the code for the scriptable object:

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

[CreateAssetMenu]
public class VectorValue : ScriptableObject
{
    public Vector3 initialValue;
}

Here is the script that I used for changing scenes in buildings:

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneChanger : MonoBehaviour
{
    #region Scene change variables
        // Public variables
        public float x = 7f;
        public float y = 7f;
        public float z = 7f;
        public bool playerNearby = false;
        public LayerMask playerMask;
        public Transform playerCheck;
        public GameObject pressEText;
        public SceneReference nextLocation;

        //Private variables
        Vector3 rotation = new Vector3(0,0,0);
    #endregion

    #region Location debug variables
        //Public variables
        public Vector3 playerPosition;
        public VectorValue playerStorage;
    #endregion

    // Update is called once per frame
    void Update()
    {
        // Creates a box where checks player inside
        playerNearby = Physics.CheckBox(playerCheck.position, new Vector3(x, y, z), Quaternion.Euler(rotation), playerMask);

        if (playerNearby)
        {
            pressEText.SetActive(enabled);
        }
        else
        {
            pressEText.SetActive(false);
        }

        if (playerNearby && Input.GetKeyDown(KeyCode.E))
        {
            playerStorage.initialValue = playerPosition;
            SceneManager.LoadScene(nextLocation);
        }
    }
}

And here is the code that is in the playerMovement script:

//Public Variables
    public CharacterController controller;
    public string enviromentScene = "SampleScene";
    public float walkSpeed = 6f;
    public float sprintSpeed = 15f;
    public float crouchSpeed = 2f;
    public float turnSmoothTime = 0.1f;
    public float gravityPlayer = -9.81f;
    public float groundDistance = 0.4f;
    public float jumpHeight = 3f;
    public float staminaValue = 100f;
    public float staminaRecoveryRate = 1f;
    public float staminaLossRate = 5f;
    public Transform tpCamera;
    public Transform groundCheck;
    public LayerMask groundMask;
    public VectorValue startingPosition;

    //Private Variables
    float turnSmoothVelocity;
    float speed = 2f;
    bool isGrounded;
    bool isSprint;
    bool isCrouch;
    Vector3 velocity;
    Vector3 moveDir;

    void Start()
    {
        transform.position = startingPosition.initialValue;
    }

Can someone help me? How to make it 100% of the time work?

CodePudding user response:

So i figured it out. If someone is having the same problem has me i recommend you check if you are using a Player Controller.

Player controller and transforms don't work that well apperantly, so if you want to set up a game object transform that has a controller associated with it, try disable it before the tranform and enable it after the transform.

It works for me!

  • Related