一般在遊戲中,主角或者怪物會受到減速效果,或者攻擊速度減慢等類似的狀態。本身動作減速的同時,銜接在角色上的特效也需要改變相應的播放速度。一般特效有三個遊戲組件:
關鍵點就是改變Animator,Animation和Particle System的Speed這個速度變量。就可以隨意的控制特效的播放速度了。
代碼如下:
using UnityEngine;
using System.Collections;
public class EffectSpeedControl : MonoBehaviour
{
public float time = 1;//銷燬時間
float mSpeed = 1f;//播放時間
void Start()
{
ChangeSpeed(transform);
}
void ChangeSpeed(Transform tr)
{
if (tr.animation != null)
{
foreach (AnimationState an in tr.animation)
{
an.speed = mSpeed;
}
}
Animator ani = tr.GetComponent<Animator>();
if (ani != null)
{
ani.speed = mSpeed;
}
if (tr.particleSystem != null)
{
var main = tr.particleSystem.main;
main.simulationSpeed = mSpeed;
}
for (int i = 0; i < tr.childCount; i++)
{
ChangeSpeed(tr.GetChild(i));
}
}
public float Speed
{
get
{
return mSpeed;
}
set
{
mSpeed = value;
ChangeSpeed(transform);
}
}
void Update()
{
time -= Time.deltaTime * mSpeed;
if (time < 0)
{
Destroy(this.gameObject);
}
}
}