I am currently having a really annoying problem, though it has been there since I can remember. I only now noticed it again after getting back into switching scenes.
Basically what happens, is every time I take the player to a different scene when they go through a door, the camera is slightly upwards from it’s point I set in the editor, and then it moves downwards to the player. The camera moves higher when entering the second scene than it does when entering the first.
I have tried aligning everything in both scenes based on first the camera, then the spawn point, and then the player. But it still happens exactly the same.
The camera (along with the player) is a singleton, and has a script for following the player with a smoothed effect.
Along with aligning everything by the camera, I tried playing it, waiting for the camera to stop moving on each scene, and saving the position in the editor afterwards, but it keeps happening.
Just noticed that the player actually gets thrown up for some reason, for a split second. Sometimes he doesn’t come back down and just floats down due to gravity… I have absolutely zero clue to why it’s happening.
Whether the player floats or snaps back to the ground, appears to be random. Though the player never floats when entering the scene they started on.
I tried playing with colliders’ positions and dimensions, but that doesn’t seem to affect it.
I need to get this finished by the end of the week (preferably). And I can’t with the player teleporting into the air like that. Please this is urgent. Even a suggestion on what to do, or what could be causing this, will help.
You haven’t provided enough information for anyone to help.
How are you setting the player position on scene change? (the actual code)
Is the player an object that exists between scenes, or something that is instantiated when entering the new scene?
Is this unexpected player movement happening at the end of a scene, or at the beginning of the next scene?
There are 2 scripts. “LoadArea” and “SpawnPoint”. SpawnPoint is attached to an empty child object of a door, and LoadArea is attached to the door objects.
SpawnPoint contains a variable “pointName”, which is the name for that spawn point (e.g. “HouseSceneLeftDoor”). The variable is set for each child object, in the inspector. The script also sets a variable called “startPoint” (contained in a PlayerController script) to its pointName variable.
LoadArea contains “exitPoint” and “sceneToLoad” variables. The exitPoint is the same as the pointName variable in the SpawnPoint script. The script loads the scene specified by sceneToLoad, and sets the player’s “startPoint” variable to its exitPoint variable.
pointName is basically a reference variable, and exitPoint is the LoadArea script’s representation of that variable (another string holding the same information).
This was all taken from a very old set of tutorial videos, and the code was neither commented nor explained very well. The tutorials were for a top-down 2D game, where both x and y represented a position on the ground, whereas in my case, it’s a sidescroller 2D game. Looking at the code now, there are probably some things that are overlapping in functionality, but without going over those old tutorials and listening to everything the person said, I can’t figure out exactly why those certain things would cause problems like the player getting launched in the air.
Here are both scripts, and links to those videos:
SpawnPoint
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPoint : MonoBehaviour {
private PlayerController thePlayer;
public string pointName;
void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
if (thePlayer.startPoint == pointName) {
thePlayer.transform.position = transform.position;
}
}
}
LoadArea
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using PixelCrushers.DialogueSystem;
public class LoadArea : MonoBehaviour {
bool inTrigger;
public string sceneToLoad;
public string exitPoint;
public PlayerController thePlayer;
void Awake () {
thePlayer = FindObjectOfType<PlayerController> ();
actionText = GameObject.Find ("ActionLetterText").GetComponent<Text> ();
}
void FixedUpdate () {
if (inTrigger == true) {
if (Input.GetKeyDown (KeyCode.E)) {
thePlayer.startPoint = exitPoint;
FindObjectOfType<LevelManager>().LoadLevel(sceneToLoad);
}
}
}
void OnTriggerEnter2D () {
inTrigger = true;
}
void OnTriggerExit2D () {
inTrigger = false;
}
}
Since this was in editor & general support… something I only see rarely when looking at new posts, I had no idea it was a thing
Flying in the air could be some physics interaction, maybe? You could probably test this by disabling the collider when you are on the map , just before you load the new scene.
I had the spawn point be positioned on the middle of the door, and since the player’s pivot was its bottom, it was getting put there.
Just made the player a parent of the spawn point object, set it (spawn point) to its (player’s) bottom (y=0), and put it back in the door object. Now I spawn properly.