Taking Player Input Away From the Player While a Timeline Sequence is Active in Unity — No Code Required!
Objective: To remove the player input while a Timeline sequence is playing in Unity.
I have just finished up the ‘Combining Cinemachine and Timeline to Open a Treasure Chest in Unity’ article and have noticed that the player still has full control while the sequence is playing. Let’s remove this by adding 1 simple track!
Currently we have the player approaching the treasure chest, triggering a Timeline sequence to play out. However, the issue we have now is that the player still has full control of the character and it takes away from the emphasis of the cutscene itself as it looks a bit stupid. Let’s remove this!
Adding an Animation Track to Timeline
Yes this is as easy as it gets! Let’s just add an animation to the Timeline while the sequence/cutscene is playing out and the player loses all control! I have found an ‘Idle’ animation from Mixamo and added this to the Timeline:
However, it wouldn’t be game development if we didn’t find more issues as we build out our scene:
As you can see, as the Timeline sequence plays out the character moves to the transform position of the PlayableDirector() component game object.
Lucky for us there is an easy solution!
We are going to use a ‘Clip Transform Offset’ of the Animation Clip.
While having the Animation Clip highlighted in the Timeline, head over to the Inspector and look for the ‘Clip Transform Offsets’ feature and click on the arrows symbol. Make sure that the Timeline Playhead (white line in Timeline) is at the right keyframe/seconds that you wish to apply the transform offset to.
Let’s Test It!
The behaviour we now expect is the player has movement up until they trigger the sequence, with the ‘Idle’ Animation Clip taking control of the players movement. Once the sequence is finished, movement should come back to the player:
Works great! Just needs a bit of blend tweaking =)