forked from oculus-samples/voicesdk-samples-whisperer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeroPlant.cs
123 lines (104 loc) · 3.95 KB
/
HeroPlant.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
using System.Collections;
using Meta.WitAi;
using Meta.WitAi.Json;
using UnityEngine;
using UnityEngine.Events;
namespace Whisperer
{
public class HeroPlant : ForceMovable
{
/// <summary>
/// Hero Move Setup
/// </summary>
[Header("Hero Move Setup")] [SerializeField]
private Transform _targetSpotTransform;
[SerializeField] private float _distanceThreshold = 0.35f;
public UnityEvent OnPlacedOnSpot;
/// <summary>
/// Rigidbody Move Settings
/// </summary>
[Header("Rigidbody Move Settings")] [SerializeField]
private float _moveDistance = .25f;
[SerializeField] private float _moveTime = .4f;
[SerializeField] private Easings.Functions _easing;
[SerializeField] private float _minX = -1.1f;
[SerializeField] private float _maxX = 1.1f;
[SerializeField] private float _minZ = .4f;
[SerializeField] private float _maxZ = 1.38f;
private Progress _positionProgress;
private Vector3 _startPos;
private Vector3 _targetPos;
protected override void Start()
{
base.Start();
StartCoroutine(WatchDistanceThreshold());
_positionProgress = new Progress(LerpPosition);
}
protected IEnumerator WatchDistanceThreshold()
{
while (Vector3.Distance(transform.position, _targetSpotTransform.position) > _distanceThreshold)
yield return null;
SetListeningActive(false);
OnPlacedOnSpot.Invoke();
GetComponentInChildren<Animator>().SetTrigger("Grow");
}
[MatchIntent("move")]
[MatchIntent("jump")]
[MatchIntent("pull")]
[MatchIntent("push")]
public override void ForceMove(ForceDirection direction, WitResponseNode node)
{
if(!IsSelected || !_actionState)
{
return;
}
float multiplier = 1;
var success = false;
switch (direction)
{
case ForceDirection.left:
RigidbodyMove(Vector3.left, multiplier);
success = true;
break;
case ForceDirection.right:
RigidbodyMove(Vector3.right, multiplier);
success = true;
break;
case ForceDirection.toward:
RigidbodyMove(Vector3.back, multiplier);
success = true;
break;
case ForceDirection.away:
RigidbodyMove(Vector3.forward, multiplier);
success = true;
break;
default:
success = false;
break;
}
ProcessComplete("move", success);
}
private void RigidbodyMove(Vector3 direction, float distanceMultiplier = 1)
{
_positionProgress.Pause();
_startPos = transform.position;
_targetPos = transform.position + direction * (_moveDistance * distanceMultiplier);
var clampedTargetPos = new Vector3(Mathf.Clamp(_targetPos.x, _minX, _maxX), _startPos.y,
Mathf.Clamp(_targetPos.z, _minZ, _maxZ));
_targetPos = clampedTargetPos;
_positionProgress.Play(_moveTime);
}
private void LerpPosition(float progress)
{
var easedProgress = Easings.Interpolate(progress, _easing);
var pos = Vector3.Lerp(_startPos, _targetPos, easedProgress);
rb.MovePosition(pos);
}
}
}