-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectResetter.cs
57 lines (46 loc) · 1.51 KB
/
ObjectResetter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityStandardAssets.Utility
{
public class ObjectResetter : MonoBehaviour
{
private Vector3 originalPosition;
private Quaternion originalRotation;
private List<Transform> originalStructure;
private Rigidbody Rigidbody;
// Use this for initialization
private void Start()
{
originalStructure = new List<Transform>(GetComponentsInChildren<Transform>());
originalPosition = transform.position;
originalRotation = transform.rotation;
Rigidbody = GetComponent<Rigidbody>();
}
public void DelayedReset(float delay)
{
StartCoroutine(ResetCoroutine(delay));
}
public IEnumerator ResetCoroutine(float delay)
{
yield return new WaitForSeconds(delay);
// remove any gameobjects added (fire, skid trails, etc)
foreach (var t in GetComponentsInChildren<Transform>())
{
if (!originalStructure.Contains(t))
{
t.parent = null;
}
}
transform.position = originalPosition;
transform.rotation = originalRotation;
if (Rigidbody)
{
Rigidbody.velocity = Vector3.zero;
Rigidbody.angularVelocity = Vector3.zero;
}
SendMessage("Reset");
}
}
}