- Home /
How to Rotate Editor Camera in 2D?
I have a 2D platformer game that has levels with varying directions of gravity. So levels are rotated right, some left, and some are even upside-down. The player camera automatically rotates to match the level so the player doesn't get confused while playing.
However, I'm still at a loss while editing. The level rotation makes it difficult to edit the levels because the levels are at odd angles in the SceneView. How can I rotate the editor camera so that I can see my levels at their custom rotation when editing?
Answer by shieldgenerator7 · Mar 12, 2017 at 09:55 PM
EDIT: This isn't sufficient. Yes, it rotates the view, but then the controls for manipulating objects is all weird.
I followed this tutorial and came up with this result:
You can change the SceneView's camera's rotation, but after you do so, you have to call the camera's Render()
method.
You have to use Quaternion.AngleAxis(angle, Vector3.forward)
to set the angle. Setting it to a new Quaternion is less intuitive.
To do this, you have to make a script. Here's the basic of the two scripts I created:
The Editor Object (goes in the "Assets/Editor" folder)
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(EditorCameraRotatorObject))]
public class EditorCameraRotator : Editor
{
EditorCameraRotatorObject ecro;
public void OnEnable()
{
ecro = (EditorCameraRotatorObject)target;
SceneView.onSceneGUIDelegate = rotateCamera;
}
void rotateCamera(SceneView sceneview)
{
Quaternion angle = Quaternion.AngleAxis(ecro.rotZ, Vector3.forward);
if (sceneview.camera.transform.localRotation != angle)
{
sceneview.isRotationLocked = false;
sceneview.camera.transform.localRotation = angle;
sceneview.camera.Render();
}
}
}
}
The GameObject (goes in the "Assets/Scripts" folder, must have an instance of this in a scene):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EditorCameraRotatorObject : MonoBehaviour {
public float rotZ = 0;
}
Then, to rotate the camera, just change the rotZ
value of the EditorCameraRotatorObject that you have in the scene.
Your answer
Follow this Question
Related Questions
How can my sprite move to new direction it's facing not keep going to old direction? 0 Answers
Bullet follows player mouvement when It instintiates. 1 Answer
How to fix enemy's rotation (2D) 1 Answer
Rotate within range 1 Answer
How do I make my player flip to always face the enemy and vice versa? 1 Answer