- Home /
Problem restoring proper camera rotation
Hello. I'm using the following script to restore camera's position and rotation after level editor reloads from the testing scene:
using UnityEngine;
using System.Collections;
public class LevEditCamPosInitializer : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log(LevEditGlobals.MainCameraPos);
Debug.Log(LevEditGlobals.MainCameraRot);
Debug.Log(this.gameObject.transform.rotation);
Debug.Log("-----------------------------------");
if (LevEditGlobals.MainCameraPos!=Globals.InfiniteVec3){
this.gameObject.transform.position = LevEditGlobals.MainCameraPos;
}
if (LevEditGlobals.MainCameraRot!=Globals.InfiniteQuat){
this.gameObject.transform.rotation = LevEditGlobals.MainCameraRot;
}
Debug.Log(LevEditGlobals.MainCameraPos);
Debug.Log(LevEditGlobals.MainCameraRot);
Debug.Log(this.gameObject.transform.rotation);
}
}
InfiniteVec3
and InfiniteQuat
are appropriately Vector3
and Quaternion
initialized with all values set to Mathf.Infinity
as I couldn't check against/set to null due to both Quaternion
and Vector3
being structs. Given both of these can't "naturally" reach such value, this is a safe way to check whether they've been set properly.
Anyway, back to the problem at hand. The thing is that restoring position works just nicely, but it doesn't restore rotation. I'm sure that MainCameraRot
isn't set to Globals.InfiniteQuat
, as debug statements return valid one. First debug statement returns what was put into the LevEditGlobals.MainCameraRot
, but the second always return (0.0, 0.0, 0.0, 1.0)
. I'm suspecting a clash with MouseLook script (using default one bundled with Unity, set to MouseXandY), which set rotation directly, but not sure how to fix it. Tried to play with script execution order, but no dice.
Any help? @Eric5h5 perhaps?
//edit: Here is exact debug log of this:
(Infinity, Infinity, Infinity)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:8)
(Infinity, Infinity, Infinity, Infinity)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:9)
(0.0, 0.0, 0.0, 1.0)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:10)
-----------------------------------
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:11)
(Infinity, Infinity, Infinity)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:18)
(Infinity, Infinity, Infinity, Infinity)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:19)
(0.0, 0.0, 0.0, 1.0)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:20)
(after entering and exiting test mode)
(43.7, 10.1, -49.7)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:8)
(0.0, 1.0, 0.0, 0.1)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:9)
(0.0, 0.0, 0.0, 1.0)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:10)
-----------------------------------
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:11)
(43.7, 10.1, -49.7)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:18)
(0.0, 1.0, 0.0, 0.1)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:19)
(0.0, 0.0, 0.0, 1.0)
UnityEngine.Debug:Log(Object)
LevEditCamPosInitializer:Awake() (at Assets/code/leveleditor/LevEditCamPosInitializer.cs:20)
As you can see, the rotation isn't properly restored, though it is saved as it should before going into test mode.
Answer by darkhog · May 01, 2016 at 02:00 PM
Solved it. For some reason Quaternion doesn't overload == operator properly, even though Vector3 does. I had to use Equals method instead. Unity should fix it for the sake of consistency.
Your answer
Follow this Question
Related Questions
How to make camera rotation relative to target. 3 Answers
Camera X and Y velocity HELP! 0 Answers
Using MouseLook script with LookAt function 2 Answers
Camera.WorldToScreenPoint Equivalent 1 Answer