Portals load the correct scene, but they do not put me in the correct location

I have 2 portals in my game, A and B. I have 2 scenes in my game. If I load scene 1, the portals will put me in the correct location in scene 2. If I try to go back to scene 1, I will not spawn in the correct location. If I load scene 2, the portals will put me in the correct location in scene 1, but if I try to go back to scene 2, it puts me in the wrong location.

Anyone else have this issue?

Are you getting any error messages in the console?

I get a warning about one of the downloaded pre-fabs not using the correct Nature/Soft Occlusion shader, but no error messages

Can you paste in a copy of your Portal.cs here? Don’t forget to format it with the </> button.

I have changed a few things here and there, but it should be pretty close to what is in the course.

public class Portal : MonoBehaviour
    {

        enum DestinationIdentifier
        {
            A, B
        }

        [SerializeField] int sceneToLoad = -1;
        [SerializeField] Transform spawnPoint;
        [SerializeField] DestinationIdentifier identifier;
        [SerializeField] DestinationIdentifier destination;

        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.tag == "Player")
            {
                StartCoroutine(Transition());
            }
        }

        private IEnumerator Transition()
        {
            if (sceneToLoad < 0)
            {
                yield break;
            }

            DontDestroyOnLoad(gameObject);
            yield return SceneManager.LoadSceneAsync(sceneToLoad);

            Portal otherPortal = GetOtherPortal();
            UpdatePlayer(otherPortal);

            Destroy(gameObject);
        }

        private void UpdatePlayer(Portal otherPortal)
        {
            Transform portalWayPoint = otherPortal.transform.GetChild(0);
            GameObject player = GameObject.FindWithTag("Player");
            player.transform.position = portalWayPoint.position;
         //   player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
         //   player.transform.position = otherPortal.spawnPoint.position;
         //   player.transform.rotation = otherPortal.spawnPoint.rotation;
        }

        private Portal GetOtherPortal()
        {
            foreach (Portal portal in FindObjectsOfType<Portal>())
            {
                if (portal.identifier == destination)
                {
                    print(portal.name);
                    return portal;
                }
            }
            return null;
        }
    }
}

You might want to add this in GetOtherPortal as the first line in the Foreach loop:

if (portal==this) continue;

This will rule out the portal finding itself.

it was a combination of adding that line and changing my UpdatePlayer from

        private void UpdatePlayer(Portal otherPortal)
        {
            Transform portalWayPoint = otherPortal.transform.GetChild(0);
            GameObject player = GameObject.FindWithTag("Player");
            player.transform.position = portalWayPoint.position;
        }

to

        private void UpdatePlayer(Portal otherPortal)
        {
            GameObject player = GameObject.FindWithTag("Player");
            player.transform.position = otherPortal.spawnPoint.position;
            player.transform.rotation = otherPortal.spawnPoint.rotation;
        }

Thank you so much for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms