Skip to content

Instantly share code, notes, and snippets.

@attrition
Created May 4, 2012 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save attrition/2595539 to your computer and use it in GitHub Desktop.
Save attrition/2595539 to your computer and use it in GitHub Desktop.
Unity3D Scene View Chase Camera
using UnityEngine;
using UnityEditor;
public class SceneChaseCam : EditorWindow
{
bool active = false;
bool followSelection = false;
Transform toFollow;
// Add menu named "Scene Chase Cam" to the Window menu
[MenuItem("Window/Scene Chase Cam")]
static void Init()
{
// Get/create window and focus
(EditorWindow.GetWindow<SceneChaseCam>()).Focus();
}
void OnGUI()
{
// basic options
active = EditorGUILayout.Toggle("Active:", active);
followSelection = EditorGUILayout.Toggle("Follow selection:", followSelection);
// slight aesthetic gap
GUILayout.Space(10);
EditorGUILayout.LabelField("Transform to follow:");
toFollow = EditorGUILayout.ObjectField(toFollow, typeof(Transform), true) as Transform;
// automatically follow selected gameobject?
if (followSelection && Selection.activeTransform != null)
toFollow = Selection.activeTransform;
// don't bother showing vector field unless we have a transform selected
if (toFollow != null)
toFollow.transform.position = EditorGUILayout.Vector3Field(toFollow.gameObject.name + " position",
toFollow.transform.position);
Repaint();
}
void Update()
{
// must be active, playing and following some object
if (active && Application.isPlaying && toFollow != null)
{
foreach (SceneView scene in SceneView.sceneViews)
{
scene.pivot = toFollow.position;
scene.Repaint();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment