Forum begins after the advertisement:


[Part 10] SceneTransition Minor Bug

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part 10] SceneTransition Minor Bug

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #16778
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    I have a Minor Bug after following the Part 10, like when the player comes from the city, it always transform it self to the Transform from the Bedroom (FromHouse on tutorial), see attached Images and Video

    View post on imgur.com
      has upvoted this post.
      #16779
      Jhan Khendrick Perez
      Level 5
      Participant
      Helpful?
      Up
      0
      ::

      This is the script relating to Part 10

      LocationEntryPoint

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class LocationEntryPoint : MonoBehaviour
      {
          [SerializeField]
          SceneTransitionManager.Location locationToSwitch;
      
          private void OnTriggerEnter(Collider other)
          {
              if (other.CompareTag("Player"))
              {
                  SceneTransitionManager.Instance.SwitchLocation(locationToSwitch);
              }
          }
      }

      Location Manager

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class LocationManager : MonoBehaviour
      {
          public static LocationManager Instance { get; private set; }
      
          public List<StartPoint> startPoints;
      
          public void Awake()
          {
              if(Instance != null && Instance != this)
              {
                  Destroy(this);
              }
              else
              {
                  Instance = this;
              }
          }
      
          public Transform GetPlayerStartingPosition(SceneTransitionManager.Location enteringFrom)
          {
              StartPoint startingPoint = startPoints.Find(x => x.enteringFrom == enteringFrom);
              return startingPoint.playerStart;
          }
      }

      SceneTransition Manager

      using System;
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using UnityEngine.SceneManagement;
      
      public class SceneTransitionManager : MonoBehaviour
      {
          public static SceneTransitionManager Instance;
      
          public enum Location { Bedroom, PlantingArea, TestCity}
          public Location currentLocation;
      
          Transform playerPoint;
      
          private void Awake()
          {
              if (Instance != null && Instance != this)
              {
                  Destroy(gameObject);
              }
              else
              {
                  Instance = this;
              }
      
              DontDestroyOnLoad(gameObject);
      
              SceneManager.sceneLoaded += OnLocationLoad;
      
              playerPoint = FindObjectOfType<PlayerMove>().transform;
          }
      
          //Switch the player to another scene
          public void SwitchLocation(Location locationToSwitch)
          {
              SceneManager.LoadScene(locationToSwitch.ToString());
          }
      
          //Called when a scene is loaded
          public void OnLocationLoad(Scene scene, LoadSceneMode mode)
          {
              //The location the player is coming from when the scene loads
              Location oldLocation = currentLocation;
      
              //Get the new location by converting the string of our current scene into a Location enum value
              Location newLocation = (Location)Enum.Parse(typeof(Location), scene.name);
      
              //If the player is not coming from any new place, stop executing function
              if (currentLocation == newLocation) return;
      
              //Find the start point
              Transform startPoint = LocationManager.Instance.GetPlayerStartingPosition(oldLocation);
      
              //Disable the player's CharacterController component
              CharacterController playerCharacter = playerPoint.GetComponent<CharacterController>();
              playerCharacter.enabled = false;
      
              //Change the player's position to the start point
              playerPoint.position = startPoint.position;
              playerPoint.rotation = startPoint.rotation;
      
              //Re-enable player character controller so player can move
              playerCharacter.enabled = true;
      
              //Save the current location that we just switched to
              currentLocation = newLocation;
          }
      }

      StartPoint

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      [System.Serializable]
      public struct StartPoint
      {
          public SceneTransitionManager.Location enteringFrom;
      
          public Transform playerStart;
      }

      Basically the same scripts

      #16780
      Tarha Apustaja
      Level 1
      AI Assistant
      (BETA)
      Helpful?
      Up
      0
      ::

      It seems like you have shared scripts related to scene transitioning and location management in Unity. If you have any specific questions or issues regarding these scripts, feel free to ask!

      #16781
      Terence
      Level 30
      Keymaster
      Helpful?
      Up
      0
      ::

      So please provide advice Mr. Apustaja. The poster already said that he has a Minor Bug after following the Part 10. When the player comes from the city, he always uses the position in from the Bedroom.

        has upvoted this post.
        #16782
        Tarha Apustaja
        Level 1
        AI Assistant
        (BETA)
        Helpful?
        Up
        0
        ::

        Hello, User 1! Thank you for reaching out to me for advice. It sounds like the player’s position is not being correctly updated when transitioning from the city to the bedroom.

        One common reason for this issue could be related to how the player’s position is being saved or loaded during the transition. Make sure that the player’s position is being properly updated and saved when leaving the city and loaded correctly when entering the bedroom.

        Additionally, check for any code that might be overriding the player’s position when transitioning between these two locations. It’s important to ensure that the player’s position is being handled consistently throughout the game to avoid

        #16783
        A_DONUT
        Level 6
        Moderator
        Helpful?
        Up
        0
        ::

        Doubt solved already?

        #16784
        Jhan Khendrick Perez
        Level 5
        Participant
        Helpful?
        Up
        0
        ::

        Will test

        #16785
        Jhan Khendrick Perez
        Level 5
        Participant
        Helpful?
        Up
        2
        ::

        Silly me, I forgot to correctly set the Current Location in SceneTransitionManager

        I am on the planting area scene and the Current location is set to Bedroom

        Solved now

        and 1 other person have upvoted this post.
        #16791
        Tarha Apustaja
        Level 1
        AI Assistant
        (BETA)
        Helpful?
        Up
        0
        ::

        Hey there, A_DONUT! How can I assist you today? If you have any programming questions or need help with anything tech-related, feel free to ask!

      Viewing 9 posts - 1 through 9 (of 9 total)
      • You must be logged in to reply to this topic.

      Go to Login Page →


      Advertisement below: