Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
EvadeInteraction.cs
Go to the documentation of this file.
1using UnityEngine;
2
3namespace Spelunx.Vive {
5 [SerializeField] private float triggerDistance = 2.0f; // Distance at which it moves away
6 [SerializeField] private float evadeDistance = 3.0f; // How far it moves away
7 [SerializeField] private float movementSpeed = 3.0f; // Speed of movement
8
9 private Vector3 evadeDestination = Vector3.zero;
10 private Vector3 evadeDirection = Vector3.zero;
11 private bool isEvading = false;
12
13 public void SetTriggerDistance(float triggerDistance) { this.triggerDistance = triggerDistance; }
14 public float GetTriggerDistance() { return triggerDistance; }
15
16 public void SetEvadeDistance(float evadeDistance) { this.evadeDistance = evadeDistance; }
17 public float GetEvadeDistance() { return evadeDistance; }
18
19 public void SetMovementSpeed(float movementSpeed) { this.movementSpeed = movementSpeed; }
20 public float GetMovementSpeed() { return movementSpeed; }
21
22 private void Update() {
23 if (isEvading) {
24 Evade();
25 } else {
26 Idle();
27 }
28 }
29
30 private void Idle() {
31 if (target == null) return;
32 if (triggerDistance * triggerDistance < (transform.position - target.position).sqrMagnitude) { return; }
33
34 Vector3 thisPosition = new Vector3(transform.position.x, 0.0f, transform.position.z);
35 Vector3 targetPosition = new Vector3(target.position.x, 0.0f, target.position.z);
36 Vector3 targetToThis = thisPosition - targetPosition;
37
38 evadeDirection = (targetToThis.sqrMagnitude < Mathf.Epsilon) ? Vector3.forward : targetToThis.normalized;
39 evadeDestination = transform.position + evadeDirection * evadeDistance;
40 isEvading = true;
41 }
42
43 private void Evade() {
44 Vector3 thisToDestination = evadeDestination - transform.position;
45 if (thisToDestination.sqrMagnitude < 0.1f) {
46 isEvading = false;
47 return;
48 }
49
50 transform.LookAt(transform.position + evadeDirection);
51 transform.Translate(thisToDestination.normalized * movementSpeed * Time.deltaTime, Space.World);
52 }
53 }
54}
void SetMovementSpeed(float movementSpeed)
void SetEvadeDistance(float evadeDistance)
void SetTriggerDistance(float triggerDistance)