Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Using a Timeline on prefabs

Discussion in 'Timeline' started by Aequitatis, Aug 25, 2017.

  1. Aequitatis

    Aequitatis

    Joined:
    Jan 24, 2014
    Posts:
    12
    Hello!

    In my game, enemies use the navigation framework to run for an exit. One of the paths involves a navmesh link - the enemy jumps on to a trampoline and bounces to a higher level. I thought this jump, jump, land animation would be a good use for timelines, so I've tried it out.

    I'm having trouble. The enemies are created at run time, so how do I create the timeline with a prefab? If I try dragging the prefab on to the timeline I get a "object is disabled" message. I've tried putting the director on the enemy, but that seems a poor place to put it as the timeline has to animate the enemy (animation and translation) and the trampoline (animation). It will also likely have to deactivate the nav agent and reactivate it, but I haven't got that far yet.

    Thanks for the help!
     
  2. Andy-Touch

    Andy-Touch

    A Moon Shaped Bool Unity Technologies

    Joined:
    May 5, 2014
    Posts:
    1,018
    Will the Playable Director component be added to an object in the scene (Such as the Trampoline) or on to the enemy Prefabs that are spawned runtime?
     
  3. Aequitatis

    Aequitatis

    Joined:
    Jan 24, 2014
    Posts:
    12
    I'm flexible on where it goes, as long as it works. My levels are contained areas within my scene - several levels per scene. I would ideally like the Playable Director to be attached to the level, as that is the most logical container.

    Another complication is that the level area is also spawned at runtime when the player explores their way to the right location. So yes, one prefab level spawns prefab enemies. How would you suggest I structure this?
     
  4. Aequitatis

    Aequitatis

    Joined:
    Jan 24, 2014
    Posts:
    12
    Bump. Is there a recommended way to resolve this? I basically want a timeline to target a prefab rather than a scene object.

    I could potentially have a "silent enemy" that I activate, run the timeline, deactivate and then move the original enemy into the new position so it appears seamless, but I would prefer not to hack it if I can help it. Thanks!
     
  5. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    184
    bump again:
    I want my player to be able to walk up to a box and press F, this should trigger a timeline where the player get on top of the box, and then continue from there... Like a "mini cutscene".
     
  6. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    70
    2020 bump
     
  7. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    70
    This code goes someway to solving the problem. Before you play the timeline you need to rebind the timeline tracks to their animator or game object or whatever.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Playables;
    3. using UnityEngine.Timeline;
    4.  
    5. public sealed class PlayableUtil : MonoBehaviour
    6. {
    7.     public static void BindAnimator(PlayableDirector director, string trackName, Animator animator)
    8.     {
    9.         TimelineAsset timeline = director.playableAsset as TimelineAsset;
    10.         foreach (var track in timeline.GetOutputTracks())
    11.         {
    12.             if (track.name == trackName)
    13.             {
    14.                 director.SetGenericBinding(track, animator);
    15.                 break;
    16.             }
    17.         }
    18.     }
    19.  
    20.     public static void BindActivationTrack(PlayableDirector director, string trackName, GameObject gO)
    21.     {
    22.         TimelineAsset timeline = director.playableAsset as TimelineAsset;
    23.         foreach (var track in timeline.GetOutputTracks())
    24.         {
    25.             if (track.name == trackName)
    26.             {
    27.                 director.SetGenericBinding(track, gO);
    28.                 break;
    29.             }
    30.         }
    31.     }
    32.  
    33.     public static void BindAudioSource(PlayableDirector director, string trackName, AudioSource audioSource)
    34.     {
    35.         TimelineAsset timeline = director.playableAsset as TimelineAsset;
    36.         foreach (var track in timeline.GetOutputTracks())
    37.         {
    38.             if (track.name == trackName)
    39.             {
    40.                 director.SetGenericBinding(track, audioSource);
    41.                 break;
    42.             }
    43.         }
    44.     }
    So in my code, just before I play the timeline I have this:

    Code (CSharp):
    1. PlayableUtil.BindAnimator(director, "CharacterAnimator1", character.animator);
    2.         PlayableUtil.BindAudioSource(director, "CharacterVoice", character.audioSource);
    The timeline freaks out if you have the editor version of your character and the runtime version of your character as children of the PlayableDirector at the same time so you need to be sure to destroy your editor version before playing the Playable.

    I have attached the destroy script I use. You just need to add it to the version of your character (or whatever your timeline is bound to) that you use to preview timeline playables in the editor.

    I've seen elsewhere someone suggested creating a custom clip track or marker track so you can rebind and reparent from the timeline. This might be a good way to go if you don't want to use the code every time.
     

    Attached Files:

unityunity