Search Unity

Deactivate Object with Timeline

Discussion in 'Timeline' started by Vallar, Apr 27, 2019.

  1. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Hi,

    I know of the activation track in Unity to activate objects, but is there a deactivation track? For example I want to deactivate one camera and enable the second at certain points in the track and reversing that later on.

    Can Timeline do that?

    Thanks.
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    There isn't a deactivation track built-in, but it isn't complicated to create one if that's the route you want to go. There may be one on the asset store or GitHub that I'm unaware of.

    There are options on the track that let you leave the object in what ever state you want when the timeline stops, so 2 activation tracks can be used to accomplish what you are looking for, if you use multiple clips on a track so the gap represents the area of deactivation.
     
    andrew-lukasik likes this.
  3. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Thanks for your reply. That is a bit unfortunate.

    Basically I have a case where the player is going to inspect an object in the game (during gameplay) so I want to use Timeline to deactivate the main camera and then switch to the inspection camera, move the latter to a certain point to look at the inspection item. Thus there is no set time for when each camera will be active or not (player can inspect an item for 1 minute or 10 minutes).

    I was able to achieve this without Timeline but I thought -- given we have a few situations like this, I would use Timeline to streamline the process.
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Ah, I understand. You are look for event-based sequencing, not time-based. Timeline isn't well suited to that yet, that usually can be done with script or more visual-scripting-esque tools.
     
  5. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Interesting, thanks for clarifying that, I thought Timeline could work a blend between the two. Back to scripts.
     
  6. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Hey Vallar,

    We recently switched to using Timelines for more and more of our gameplay effects, and once we got used to it writing small Tracks+Clips to do these little scripting tasks worked out very well.

    Take a look at my post here: https://forum.unity.com/threads/cod...ic-at-the-beginning-and-end-of-a-clip.661315/ for boilerplate code to get you going.

    Simply put, where it says Debug.Log("Clip started!") and Debug.Log("Clip done!" is where you'd add the logic to disable a game object.

    Now to make it work with GameObject references, the way I've been doing it (maybe there is a more elegant way) is to declare your external reference in your Clip PlayableBehaviour (think of this as the logic that consumes values on the clip and does something with them, except in your case you don't care about interpolated values on the clip just rather the fact that the clip is currently within scope of the current frame):

    Code (csharp):
    1. public class DeactivateBehaviour : PlayableBehaviour
    2. {
    3.     public GameObject Target;
    4.  
    Then you can use Target.SetActive(false) at the "Clip started" line and Target.SetActive(true) at the "Clip done!" line. Note, don't confuse the Clip PlayableBehaviour with the Mixer PlayableBehaviour. In my link the Mixer has only one empty function, don't put anything there.

    All thats left is to expose the reference on your Clip class (I'll give the more complete code here):
    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4. using UnityEngine.Timeline;
    5.  
    6. [Serializable]
    7. public class DeactivateClip : PlayableAsset, ITimelineClipAsset
    8. {
    9.     public ExposedReference<GameObject> Target;
    10.  
    11.     // Create the runtime version of the clip, by creating a copy of the template
    12.     public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    13.     {
    14.         ScriptPlayable<DeactivateBehaviour> playable = ScriptPlayable<DeactivateBehaviour>.Create(graph);
    15.  
    16.         DeactivateBehaviour playableBehaviour = playable.GetBehaviour();
    17.  
    18.         playableBehaviour.Target = Target.Resolve(graph.GetResolver());
    19.  
    20.         return playable;
    21.     }
    22.  
    23.     // Use this to tell the Timeline Editor what features this clip supports
    24.     public ClipCaps clipCaps
    25.     {
    26.         get { return ClipCaps.None; }
    27.     }
    28. }
    29.  
    And that should be it! Use the rest of the boilerplate code from the original link.

    ** edit fixed the class name on the clip **
     
    Last edited: Apr 29, 2019
  7. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    This looks like exactly what we are after. Some of it does go above my head only because I am fairly new to Timeline, I'll dig deeper today into the tutorials that talk about creating custom Playables and will try your approach.

    Thanks a lot for the links and the scripts :)
     
  8. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    If you get stuck, just ask, but that linked thread should get you 90% of the way, with the missing 10% being the GameObject Target reference described here :)
     
  9. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Thanks, will do :)