After the load of the scene the player is spawning at spawn point but a second later it teleports to the scene starting position. i am using the navmesh.warp() and i tried disabling the player controller with no succession
Is this happening when you start the game, or when transitioning between scenes?
Are you getting any errors in the inspector?
Thanks brian for replaying , the problem is only when transition from scene to scene happens
Are you getting any errors in the inspector?
Paste in your Portal.cs and we’ll take a look.
- Forum User Guides : How to apply code formatting within your post
i do not get any errors in the inspector . code:
using RPG.Controller;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;
namespace RPG.SceneManagment
{
public class Portal : MonoBehaviour
{
[SerializeField] int sceneToLoad = -1;
[SerializeField] Transform spawnPoint;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
StartCoroutine(SceneTransition());
}
}
private IEnumerator SceneTransition()
{
DontDestroyOnLoad(gameObject);
yield return SceneManager.LoadSceneAsync(sceneToLoad);
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
GameObject player = GameObject.FindWithTag("Player");
player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
player.transform.rotation = otherPortal.spawnPoint.rotation;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsOfType<Portal>())
{
if (portal == this) continue;
return portal;
}
return null;
}
}
}
Is there any chance you got a warning saying something to the effect that
DontDestroyOnLoad cannot be applied to GameObjects which are not at the root of the heirarchy?
Make sure that the portal is not a child of another GameObject, in other words that it is in the root of the scene. Otherwise it won’t be moved to the DontDestroyOnLoad scene and it will get destroyed during the scene load (causing the coroutine to stop before updating the player).
i do not get any warnings and both portals are in the root . i did an experiment and i found out that if you do this:
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
StartCoroutine(SceneTransition());
print("triggered")
}
}
it will print twice for some reason although the player is not touching the portal again. maybe theres a connection to this?
If it’s hitting twice, there are a few possibilities…
- There are two portals in the same spot (or perhaps two colliders?)
- There are additional colliders on the Player
- A portal’s spawn point is too close to the portal itself.
Let’s modify the Debug a bit…
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
StartCoroutine(SceneTransition());
print($"Portal {gameObject.name} has been triggered by {other.gameObject.name");
}
}
its triggering the same portal twice:
This is the cause of the strange behaviour, we’re getting two Transition coroutines, and the scene is actually loading twice causing crosstalk.
Now the tricky part is figuring why…
- Make sure that there aren’t two portals on the same location
- Make sure that there is only one collider on the portal.
A quick fix that can prevent the issue (but it’s not a true fix, it’s a patch):
Add a bool to the class variables
bool inTransition = false;
Then for Transition, make the first two lines read
if(inTransition) return;
inTransition=true;
This should prevent the double transition, assuming that the issue is the same portal springing twice. If this doesn’t resolve the issue, then there is another portal that is being triggered at the same time.
i checked , there is only one portal with one collider in each scene . the problem still occurs. the scene transition cannot get a return but a yield return, does it matter?
if (inTransition) yield return
inTransition = true;
if it doesnt matter the patch didnt work
Zip up your project and upload it to https://gdev.tv/projectupload and I’ll take a look at it.
Be sure to remove the Library folder to conserve space.
i uploaded it. hope you could fix the issue and thank you very much
This was actually relatively simple…
There were two colliders on the player.
Delete one of the colliders and you should be good to go.
thank you so much brian , i dont even know how i missed this… its working perfectly fine now!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.