使用unity Timeline工具制作“子弹时间”效果

子弹时间,就是黑客帝国中,可以看得到子弹运动那种。使用Timeline加速或者减速游戏里得时间,这样就不需要更改运动代码也可以获得对象运动速度的改变。

首先,创建一个Timeline Editor窗口:

Window->Timeline Editor;

创建一个空对象用来存放一个新的Timeline,这个空对象类似于一般UIManager,EventManager差不多的功能,把空对象命名为PlayableDirector,给PlayableDirector创建一个Timeline后,Inspector面板就会有一个PlayableDirector组件:

我们在Timeline Editor中add一个playable Track用来管理时间代码:

回到scene中,随便创建一个对象,比如:Cube。然后写一个旋转的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour 
{
	void Update () 
    {
        transform.Rotate(transform.up, 200 * Time.deltaTime);
	}
}

Rotate.cs挂在Cube上;

然后写我们的子弹时间脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class BulletTimePlayable : BasicPlayableBehaviour
{
	public float BulletTimeTimeScale;

	private float _originalTimeScale = 1f;

	public override void ProcessFrame(Playable playable, FrameData info, object playerData) 
	{
        //检查是否在播放,防止在短条前开始
		if (playable.GetTime() <= 0)
			return;
        Time.timeScale = Mathf.Lerp (_originalTimeScale, BulletTimeTimeScale, (float)(playable.GetTime() / playable.GetDuration()));
	}
	public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        _originalTimeScale = Time.timeScale;
    }
}

把BulletTimePlayable.cs拖到Timeline Editor中的Playable中,调整短条的长短,使用ctrl + D可以复制短条,复制出多个短条,给每个短条赋予不同的时间参数,运行时,就可以看到时间加速和减速的效果:

使用unity Timeline工具制作“子弹时间”效果_第1张图片

在2中的参数中,大于1为加速,小于1为减速



我的微信公众号;

使用unity Timeline工具制作“子弹时间”效果_第2张图片

公众号已接入智能回复,欢迎怼我

你可能感兴趣的:(unity)