- Home /
Seemingly random bug when positioning player on scene start
Hi,
I'm having trouble with a script I've put together that's meant to position the player on the start of a new scene, depending on the previous scene they were in. The name of the previous scene is stored in a global variable, and this script checks for it and if found moves the player to the position of the script object. The "Player" in this case is an empty gameobject prefab that's parent to both a Character Controller and a Camera.
If I test this in a level, it mostly works fine and moves the entire prefab to the new location as intended. However - every once in a while, and seemingly at random, it only moves the Prefab and Camera and leaves the Character Controller in the original scene position.
I'm trying out different workarounds but would really appreciate being able to find out just what's going on, for my own knowledge if nothing else. The script I'm using is below - "gPrevLevel" is the previous level string. myLastLevelName is the level name being checked against.
using UnityEngine;
using System.Collections;
public class LevelStartPosition : MonoBehaviour
{
public string myLastLevelName;
public GameObject myPlayer;
public bool myRotationFlag = true;
private void Awake()
{
if (GlobalVar.gPrevLevel == myLastLevelName)
{
myPlayer.transform.position = transform.position;
if (myRotationFlag == true)
{
myPlayer.transform.rotation = transform.rotation;
}
}
}
}
I don't understand what are you saying when you wrote:
The "Player" in this case is an empty gameobject prefab that's parent to both a Character Controller and a Camera.
Any gameObject can have only one parent.
Character Controller is a component. Which means it should sit on this empty gameObject and not on another gameObject.
In general, any fps or 3rd person character should be as such:
EmptyGameObject with CharacterController component,
MainCamera as a child of this empty,
A 3d model as a child of that camera
Sorry for confusion - in this case it means an empty (i.e. no components) gameobject which contains two different child gameobjects. One of these is the Character Controller and the other is the Main Camera. They're both being kept inside the one empty object for the sake of convenience when storing as a prefab.
But then the whole logic should be wrong.. Or at least a big chunk of it..
How do you move the player? Because if it's not on the empty gameobject than it would literally move without it's parent.
I honestly believe that if you re-order your Player structure to be as I wrote above, it should solve this issue.
No, it works fine in game, I think maybe too much emphasis is being put on me calling the container prefab the "Player" in the post. The Character Controller object it contains is also what has the mesh and animations attached to it and what actually moves around. The Camera object it also contains has a script to automatically look at and follow the Controller. They don't have any script dependencies on the parent container object at all. That object is only used to be able to move them both around at the same time and to keep them in a certain position relative to each other when adding the player to a level.
Your answer
Follow this Question
Related Questions
Player position with script and screen sizes 0 Answers
Move object with Translate on X axis (with mouse) 0 Answers
Offset position to a target 3 Answers
My Position suddenly changes 0 Answers
Is there an elegant way of limiting camera's position? 1 Answer