Home > Software design >  Position x and z isn't working for loading next scene, any ideas on why?
Position x and z isn't working for loading next scene, any ideas on why?

Time:06-10

I'm using unity HDRP and I'm trying to make a game where you go from the menu (which is a scene) to scene 1. After that every time you are within certain coordinates the next scene will be added. And at some point the first scene will be deleted. I have done this since it lags too much if I have just one scene with the whole map in it. I separated the map into 5 scenes which will load in so there is only 3 scenes at a time.

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

public class SceneLoaderScript : MonoBehaviour
{
    public bool loadnextScene = false;
    public bool isLoaded2 = false;
    public bool isLoaded3 = false;
    public bool isLoaded4 = false;
    public bool isLoaded5 = false;
    public GameObject You;

    private void Start()
    {
        You = GameObject.Find("You");
    }
    private void Update()
    {
        Vector3 SceneLoaderPos = (You.transform.transform.position).normalized;
        if (SceneLoaderPos.x > 38 & SceneLoaderPos.x < 62 & SceneLoaderPos.z > 42 & SceneLoaderPos.z < 44)
        {
            scene2();
        }
        if (SceneLoaderPos.x < 16 & SceneLoaderPos.x > 70 & SceneLoaderPos.z == 11)
        {
            scene3();
        }
        if (SceneLoaderPos.x < 16 & SceneLoaderPos.x > 70 & SceneLoaderPos.z == 11)
        {
            scene4();
        }
    }
    private void scene2()
    {
        Debug.Log("lol");
        if (isLoaded2 == false)
        {
            
            SceneManager.LoadScene("Scene 2", LoadSceneMode.Additive);
        }
        else if (isLoaded2 == true)
        {
            SceneManager.UnloadSceneAsync("scene 2");
        }
    }
    private void scene3()
    {
        if (isLoaded3 == false)
        {
            SceneManager.LoadScene("Scene 3", LoadSceneMode.Additive);
            SceneManager.UnloadSceneAsync("scene 1");
        }
        else if (isLoaded3 == true)
        {
            SceneManager.UnloadSceneAsync("scene 3");
        }
    }
    private void scene4()
    {
        if (isLoaded4 == false)
        {
            SceneManager.LoadScene("Scene 4", LoadSceneMode.Additive);
            SceneManager.UnloadSceneAsync("scene 1");
        }
        else if (isLoaded4 == true)
        {
            SceneManager.UnloadSceneAsync("scene 4");
        }
    }

}

You load in scene 1 in an different script and this script is put on the character you play ( who is called You)

any ideas why it isn't working? I tried to put in a Debug.Log("hi") in the scene2 function to see if it ever went in and nothing appeared.

CodePudding user response:

The main issue above seems to be the use of normalize on the position vector.

Normalization consists of dividing every entry in a vector by its magnitude to create a vector of length 1 known as the unit vector.

For reference

new Vector3(100, 200, 300).normalized  ==  new Vector3(0.3, 0.5, 0.8)

As a result of the normalization, the ranges being compared are likely well above the normalized value.

The conditions themselves are using the logical and operator, which is likely not what you want based on the usage. This will evaluate all operands, even when the resulting value will not change upon further evaluation.

The & operator evaluates both operands even if the left-hand operand evaluates to false, so that the operation result is false regardless of the value of the right-hand operand.

Instead use the conditional logical and operator && aka short-circuiting logical and. This will not evaluate the right side if the left side is false.

And lastly, do not compare float values to distinct values. Because of floating point inaccuracies, a float usually will not be that exact number. Instead you want to incorporate an epsilon value. Unity has a built in function in the Mathf static class, Mathf.Approximately.

Compares two floating point values and returns true if they are similar.

  • Related