Tanoda
Alap/LerpToMe.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using NaughtyAttributes;
5using UnityEngine;
7#if !UNITY_WEBGL
8using Valve.VR;
9using Valve.VR.InteractionSystem;
10#endif
11
12public class LerpToMe : MonoBehaviour
13{
14 public bool reparent = false, skipDestroy = false;
15 [ShowIf("reparent")]
16 public Transform newParent;
17 public UnityEvent onFinished;
18 private GameObject target;
19
20 public void DoTheLerp(GameObject o)
21 {
22 target = o;
23 StartCoroutine(Lerp());
24 }
25
26 private IEnumerator Lerp()
27 {
28 if (target.GetComponent<ThrowableCanDisable>())
29 {
30#if !UNITY_WEBGL
31 target.GetComponent<ThrowableCanDisable>().ForceDrop();
32#endif
33 }
34 target.transform.parent = null;
35 float time = 0;
36 while (time <= 0.5f)
37 {
38 time += Time.deltaTime;
39 target.transform.position = Vector3.Lerp(target.transform.position, transform.position, time / 0.5f);
40 target.transform.rotation = Quaternion.Lerp(target.transform.rotation, transform.rotation, time / 0.5f);
41 yield return new WaitForEndOfFrame();
42 }
43 target.transform.rotation = target.transform.rotation;
44 target.transform.position = target.transform.position;
45 try
46 {
47 target.GetComponent<Collider>().enabled = false;
48 }
49 catch (Exception)
50 {
51 //ignored
52 }
53
54 if (!skipDestroy)
55 {
56 try
57 {
58 Destroy(target.GetComponent<ThrowableCanDisable>());
59#if !UNITY_WEBGL
60 Destroy(target.GetComponent<Interactable>());
61 Destroy(target.GetComponent<VelocityEstimator>());
62#endif
63 }
64 catch (Exception)
65 {
66 // ignored
67 }
68 }
69
70 if (reparent && newParent)
71 {
72 target.transform.SetParent(newParent, true);
73 }
74 onFinished.Invoke();
75 }
76}
bool skipDestroy
UnityEvent onFinished
bool reparent
Transform newParent
void DoTheLerp(GameObject o)