Tanoda
FollowMe.cs
Go to the documentation of this file.
1using System.Collections;
2using UnityEngine;
3using UnityEngine.UI;
4
5public class FollowMe : MonoBehaviour
6{
7 public GameObject Player;
8
9 Vector3 PlayerPosition = new Vector3();
10 Vector3 PlayerPositionForward = new Vector3();
11 Vector3 moveForward= new Vector3();
12 Vector3 targetPosition = new Vector3();
13 private GameObject targetObject;
14 public GameObject particleSystem;
15 public Toggle toggle;
16 public Camera VRCamera;
17 bool needToFollow = false;
18 bool isRunning = false;
19 public static FollowMe Instance { get; private set; }
20
21 // Start is called before the first frame update
22 void Start()
23 {
24 Instance = this;
25 if (MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Training && MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Optimization) toggle.gameObject.SetActive(false);
26 }
27
28 public void StopFollowing()
29 {
30 StopAllCoroutines();
31 particleSystem.GetComponent<ParticleSystem>().Stop();
32 needToFollow = false;
33 }
34
35 public void StartFollowing(Vector3 EndPosition, GameObject TargetObject)
36 {
37 targetPosition = EndPosition;
38 needToFollow = true;
39 particleSystem.GetComponent<ParticleSystem>().Play();
40 StartCoroutine(MakeGuideLine(EndPosition));
41 targetObject = TargetObject;
42
43 }
44
45 // Update is called once per frame
46 void Update()
47 {
48 PlayerPosition = new Vector3(Player.transform.position.x, Player.transform.position.y - 0.15f, Player.transform.position.z);
49 //moveForward = new Vector3(PlayerPosition.x, PlayerPosition.y, PlayerPosition.z + (Vector3.Distance(PlayerPosition, targetPosition)*0.3f));
50 var move = PlayerPosition + Player.transform.forward * (Vector3.Distance(PlayerPosition, targetPosition) * 0.6f);
51 if (targetObject != null)
52 {
53 if (isInFieldOfView(targetObject))
54 {
55 moveForward = new Vector3(move.x, move.y , move.z); /*move;*/
56 }
57 else
58 {
59 moveForward = new Vector3(move.x, move.y + 0.45f, move.z);
60 }
61 }
62
63 if ((Vector3.Distance(PlayerPosition, targetPosition) >= 0.9f) && needToFollow && !isRunning)
64 {
65 particleSystem.GetComponent<ParticleSystem>().Play();
66 StartCoroutine(MakeGuideLine(targetPosition));
67 }
68 }
69
70 IEnumerator MakeGuideLine(Vector3 EndPosition)
71 {
72
73 float time = 0;
74 float duration = 1f;
75 particleSystem.transform.position = PlayerPosition;
76
77 while (Vector3.Distance(PlayerPosition, EndPosition) >= 0.9f)
78 {
79 isRunning = true;
80 time += Time.deltaTime;
81 if (time >= duration)
82 {
83 particleSystem.GetComponent<ParticleSystem>().Pause();
84 time = 0;
85 particleSystem.transform.position = PlayerPosition;
86 particleSystem.GetComponent<ParticleSystem>().Play();
87 }
88 //particleSystem.transform.position = Vector3.Lerp(PlayerPosition, EndPosition, time/duration);
89 particleSystem.transform.position = CalculateLinearBeziearPoint(time / duration, PlayerPosition, moveForward, EndPosition);
90
91 yield return null;
92
93 }
94 isRunning = false;
95
96
97 }
98 Vector3 CalculateLinearBeziearPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
99 {
100
101 float u = 1 - t;
102 float tt = t * t;
103 float uu = u * u;
104 Vector3 p = uu * p0 + 2 * u * t * p1 + tt * p2;
105
106 return p;
107 }
108 public void TurnOnOffGuide(bool newValue)
109 {
110 if (!newValue)
111 {
112 particleSystem.GetComponent<ParticleSystem>().Stop();
113 particleSystem.SetActive(false);
114 }
115 else
116 {
117 particleSystem.GetComponent<ParticleSystem>().Play();
118 particleSystem.SetActive(true);
119 }
120 }
121
122 private bool isInFieldOfView(GameObject interactedGO)
123 {
124 Vector3 screenPoint = VRCamera.WorldToViewportPoint(interactedGO.transform.position);
125 bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
126 return onScreen;
127 }
128}
void TurnOnOffGuide(bool newValue)
Definition: FollowMe.cs:108
void StopFollowing()
Definition: FollowMe.cs:28
void StartFollowing(Vector3 EndPosition, GameObject TargetObject)
Definition: FollowMe.cs:35
Toggle toggle
Definition: FollowMe.cs:15
static FollowMe Instance
Definition: FollowMe.cs:19
Camera VRCamera
Definition: FollowMe.cs:16
GameObject particleSystem
Definition: FollowMe.cs:14
GameObject Player
Definition: FollowMe.cs:7