Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do you move the camera on editor with preview

Discussion in 'Getting Started' started by gudula, May 20, 2015.

  1. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    Hi

    When you have a game scene running and you playing it, how can you make the scene editor follow it also while you playing?

    Louis
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Shift+F. (Might be CMD+F). One of those should work.
     
  3. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    there is "Align view with selected" but there is no shortcode there?? I would like to activate "Align view with selected" when I hit an arrow key to move my character or better yet when the camera is moving on preview.
    Anybody know how to achieve this. It would really speed up development
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    As I said, Shift+F.
    Select the object you want to follow, press Shift+F, and hit play. The scene view will stay aligned with that object as it moves.
     
  5. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    Ok thanks that works. But I would like to select a camera and see the cameras view. Do you know how to see that?
    shift works but its view is of the camera on the side.
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    I don't believe there is anything built in, but you can align the scene with the camera of your choice using editor scripting.
    Here is a very quick and dirty example. It is very simple and could be done a lot fancier, but it will do what you want. ( I didn't see anything already out there, so whipped this up).
    CamSceneFollow.cs (create this script and attach it to an empty gameobject.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CamSceneFollow : MonoBehaviour {
    5.  
    6.     public bool follow_cam;
    7.     public Transform cam;
    8.  
    9. }
    10.  
    CamSceneFollowEditor.cs (create this script and make sure it is in an Editor folder.
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(CamSceneFollow))]
    5. public class CamSceneFollowEditor : Editor {
    6.  
    7.     public CamSceneFollow script;
    8.     public void OnEnable()
    9.     {
    10.         script = (CamSceneFollow)target;
    11.     }
    12.  
    13.     public override void OnInspectorGUI()
    14.     {      
    15.         if(script.follow_cam && script.cam != null)
    16.         {
    17.             SceneView.lastActiveSceneView.pivot = script.cam.position;
    18.             SceneView.lastActiveSceneView.rotation = script.cam.rotation;
    19.             SceneView.lastActiveSceneView.Repaint();
    20.         }
    21.         DrawDefaultInspector();
    22.         EditorUtility.SetDirty(script);
    23.     }
    24. }
    Drag your camera onto the cam variable in the gameObject, and click the follow_cam box when you enter play mode. Leave it off when it edit mode, as it will try to follow the camera there too. you can zoom in and out with the mouse wheel.

    Like I said, this is very simplistic, and runs a bit slow (probably not the best way to do it). But it is a start, and you can expand it.
     
    chelnok likes this.
  7. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    It would be quite nice if there was something similar built in, given that all the debug info tends to only be visible on the Scene view, not Game.
    Of course you can write stuff to make it show up in-game, but that's messy. Plus doesn't include things like the lovely shading modes and GI information you get on the Scene view.

    It's a minor thing really though, can't think of a huge amount of situations where I'd needed it.
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    That's the nice thing about being able to script the editor, when those rare cases come up, there is a way easily deal with them.

    I do agree about the game view though. It would be nice to be able to have even just gizmos you use in game view. Yea, it would be a performance hit, but still.
     
    Last edited: May 20, 2015
  9. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    Yeah it's mostly the gizmos and the new render views, I'm not aware of any easy ways of even editor scripting those.
    But yes, editor scripting is such a very useful thing.
     
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    3agle likes this.