61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class DebrisEffect : MonoBehaviour
|
|
{
|
|
[SerializeField]private float MoveSpeed= 1;
|
|
[SerializeField]private float DrowningSpeed= 1;
|
|
|
|
[SerializeField]private float AliveTime = 30;
|
|
[SerializeField]private float RotationSpeed = 1;
|
|
float aliveTimer = 0;
|
|
private Dictionary<Transform, Vector3> debris;
|
|
|
|
void Start(){
|
|
Play();
|
|
}
|
|
void Update()
|
|
{
|
|
if(debris == null){
|
|
return;
|
|
}
|
|
if(aliveTimer < AliveTime){
|
|
aliveTimer+=Time.deltaTime;
|
|
MoveDebris();
|
|
}else{
|
|
//Time to go!
|
|
gameObject.SetActive(false);
|
|
aliveTimer=0;
|
|
}
|
|
}
|
|
|
|
void MoveDebris(){
|
|
foreach(KeyValuePair<Transform, Vector3> debri in debris){
|
|
float m_moveSpeed= MoveSpeed;
|
|
if(debri.Value.z > 0){
|
|
m_moveSpeed= MoveSpeed*2;
|
|
}
|
|
debri.Key.position+= debri.Value * m_moveSpeed;
|
|
if(aliveTimer > AliveTime/3f){
|
|
debri.Key.position -= new Vector3(0,0, DrowningSpeed);
|
|
}
|
|
debri.Key.Rotate(new Vector3(0,0,0.1f) *debri.Value.magnitude * RotationSpeed);
|
|
}
|
|
}
|
|
public void Play(Vector3 position){
|
|
transform.position = position;
|
|
Play();
|
|
}
|
|
public void Play(){
|
|
aliveTimer=0;
|
|
|
|
debris = new Dictionary<Transform, Vector3>();
|
|
for(int i=0; i < transform.childCount; i++){
|
|
Vector2 movement = new Vector3(Random.Range(-1f,1f), Random.Range(-1f,1f), Random.Range(-1f,0f));
|
|
debris.Add(transform.GetChild(i), movement);
|
|
transform.GetChild(i).position = transform.position;
|
|
}
|
|
}
|
|
}
|