Player's action / frame Update
Hi,
I have created 2 scenes and I want the player to go from one scene to another by pressing a keyboard touch (E) when he is in a specific triggerable areas.
Basically, my script is :
private GameObject FindPlayer()
{
GameObject[] _player;
_player = GameObject.FindGameObjectsWithTag("Player");
return _player[0];
}
void Awake()
{
Application.targetFrameRate = 300;
_player = FindPlayer();
}
public void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
CollisionWithPlayer = true;
}
}
public void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
CollisionWithPlayer = false;
}
}
IEnumerator LoadYourAsyncScene() {
Scene currentScene = SceneManager.GetActiveScene();
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(_SceneToLoad, LoadSceneMode.Additive);
while (!asyncLoad.isDone)
{
yield return null;
}
SceneManager.MoveGameObjectToScene(_player, SceneManager.GetSceneByName(_SceneToLoad));
SceneManager.UnloadSceneAsync(currentScene);
}
public void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.E) & CollisionWithPlayer == true)
{
StartCoroutine(LoadYourAsyncScene());
}
}
The script seems to work nice for the loading/unloading of the scene, however, when i press E in the zone, quite often (3/10), nothing happens. I thought it was because I orginally used the OnTriggerStay function whch doesn't update every frame. So I swapped on FixedUpdate function but I still got the bug :(
I had the same trouble without the part where I load/unload scene :(
Btw, the others keyboard touches (P, W...) have the same problem in game but I can write properly with my keyboard.
Do you know what is wrong with the script?
Your answer
Follow this Question
Related Questions
how to stop an animation at a specific frame on animator? 1 Answer
Does framerate affect Coroutines? 1 Answer
My game seems to be running too fast for certain tasks 0 Answers
Chaotic Frame Rates Depending On Monitor 0 Answers
unity sudden freezing 0 Answers