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 to record the Cam Move Like this in Time Line?

Discussion in 'Timeline' started by cvadiablo, Jun 20, 2018.

  1. cvadiablo

    cvadiablo

    Joined:
    Jun 2, 2017
    Posts:
    19
    I want to let Camera follow the SceneView move and Rotate, and then record it in TimeLine.
    This will make it easier for Artist to better record the first person view.
    But in any case I couldn't get the position and rotation value I wanted correctly, and every time I recorded the value was wrong.
    Is there any way to achieve this feature?

    Code (CSharp):
    1. using UnityEngine.Timeline;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(CameraFollow))]
    7. public class CameraFollowEditor : Editor
    8. {
    9.     CameraFollow cameraFollow;
    10.     GUIStyle style = new GUIStyle();
    11.  
    12.     public override void OnInspectorGUI()
    13.     {
    14.         DrawDefaultInspector();
    15.  
    16.         if (GUILayout.Button("AddKeyFrame"))
    17.         {
    18.             cameraFollow.AddKeyFrame();
    19.         }
    20.  
    21.         if (GUILayout.Button("CamAlignToView"))
    22.         {
    23.             cameraFollow.MakeCamAlignToView();
    24.         }
    25.  
    26.     }
    27.  
    28.     void OnEnable()
    29.     {
    30.         cameraFollow = (CameraFollow)target;
    31.     }
    32.  
    33. }
    34.  
    35. [ExecuteInEditMode]
    36. public class CameraFollow : MonoBehaviour {
    37.  
    38.  
    39.     public bool CameraFollowOn = false;
    40.  
    41.     private Vector3 oldSceneViewPos;
    42.     private Quaternion oldSceneViewRot;
    43.     private PlayableDirector playableDirector;
    44.  
    45.     void Awake()
    46.     {
    47.         playableDirector = GetComponent<PlayableDirector>();
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         MakeCamAlignToView();
    53.     }
    54.  
    55.     public void MakeCamAlignToView() {
    56.  
    57.         if ( playableDirector != null && playableDirector.playableGraph.IsValid() == true && playableDirector.playableGraph.IsPlaying() == true)
    58.         {
    59.             Debug.Log("Previewing...");
    60.             return;
    61.         }
    62.  
    63.         if (Application.isPlaying)
    64.         {
    65.             return;
    66.         }
    67.  
    68.         if (CameraFollowOn == false)
    69.         {
    70.             return;
    71.         }
    72.  
    73.         var view = UnityEditor.SceneView.lastActiveSceneView;
    74.         if (view != null && ( oldSceneViewPos != view.camera.transform.position || oldSceneViewRot != view.camera.transform.rotation))
    75.         {
    76.             gameObject.transform.position = view.camera.transform.position;
    77.             Quaternion rot = view.camera.transform.rotation;
    78.             gameObject.transform.rotation = rot;
    79.             oldSceneViewPos = view.camera.transform.position;
    80.             oldSceneViewRot = view.camera.transform.rotation;
    81.         }
    82.     }
    83.  
    84.     public void AddKeyFrame()
    85.     {
    86.         // TODO , don't know how to do this...
    87.         var binding = playableDirector.playableAsset.outputs;
    88.         foreach (var item in binding)
    89.         {
    90.             var at = item.sourceObject as AnimationTrack;
    91.             Debug.Log("at>>>>>>>>>" + at + "//" + at.GetClips());
    92.             Debug.Log("at++++++" + at.openClipOffsetPosition + "//" + at.openClipOffsetRotation);
    93.             foreach (TimelineClip clip in at.GetClips())
    94.             {
    95.                 Debug.Log(clip.animationClip.name + "\n");
    96.             }
    97.         }
    98.     }
    99.  
    100. }
    wrongPosition.png
     

    Attached Files:

    Last edited: Jun 20, 2018