t1 / TFDContents / Assets / KinectDemos / SpeechRecognitionDemo / Scripts / BotControlScript.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (5.76 KB)
| 1 |
using UnityEngine; |
|---|---|
| 2 |
using System.Collections; |
| 3 |
|
| 4 |
// Require these components when using this script |
| 5 |
[RequireComponent(typeof (Animator))] |
| 6 |
[RequireComponent(typeof (CapsuleCollider))] |
| 7 |
[RequireComponent(typeof (Rigidbody))] |
| 8 |
public class BotControlScript : MonoBehaviour, SpeechRecognitionInterface |
| 9 |
{
|
| 10 |
// [System.NonSerialized] |
| 11 |
// public float lookWeight; // the amount to transition when using head look |
| 12 |
// |
| 13 |
// [System.NonSerialized] |
| 14 |
// public Transform enemy; // a transform to Lerp the camera to during head look |
| 15 |
|
| 16 |
[Tooltip("Overall animation speed.")]
|
| 17 |
public float animSpeed = 1.5f; // a public setting for overall animator animation speed |
| 18 |
|
| 19 |
//public float lookSmoother = 3f; // a smoothing setting for camera motion |
| 20 |
|
| 21 |
[Tooltip("Whether to use the extra curves for animation or not.")]
|
| 22 |
public bool useCurves; // a setting for teaching purposes to show use of curves |
| 23 |
|
| 24 |
|
| 25 |
private Animator anim; // a reference to the animator on the character |
| 26 |
private AnimatorStateInfo currentBaseState; // a reference to the current state of the animator, used for base layer |
| 27 |
private AnimatorStateInfo layer2CurrentState; // a reference to the current state of the animator, used for layer 2 |
| 28 |
private CapsuleCollider col; // a reference to the capsule collider of the character |
| 29 |
|
| 30 |
private SpeechManager speechManager; |
| 31 |
private float walkSpeed; |
| 32 |
private float walkDirection; |
| 33 |
private bool jumpNow; |
| 34 |
private bool waveNow; |
| 35 |
|
| 36 |
static int idleState = Animator.StringToHash("Base Layer.Idle");
|
| 37 |
static int locoState = Animator.StringToHash("Base Layer.Locomotion"); // these integers are references to our animator's states
|
| 38 |
static int jumpState = Animator.StringToHash("Base Layer.Jump"); // and are used to check state for various actions to occur
|
| 39 |
static int waveState = Animator.StringToHash("Layer2.Wave");
|
| 40 |
|
| 41 |
|
| 42 |
// invoked when a speech phrase gets recognized |
| 43 |
public bool SpeechPhraseRecognized(string phraseTag, float condidence) |
| 44 |
{
|
| 45 |
switch(phraseTag) |
| 46 |
{
|
| 47 |
case "FORWARD": |
| 48 |
walkSpeed = 0.2f; |
| 49 |
walkDirection = 0f; |
| 50 |
break; |
| 51 |
|
| 52 |
case "BACK": |
| 53 |
walkSpeed = -0.2f; |
| 54 |
walkDirection = 0f; |
| 55 |
break; |
| 56 |
|
| 57 |
case "LEFT": |
| 58 |
walkDirection = -0.2f; |
| 59 |
if(walkSpeed == 0f) |
| 60 |
walkSpeed = 0.2f; |
| 61 |
break; |
| 62 |
|
| 63 |
case "RIGHT": |
| 64 |
walkDirection = 0.2f; |
| 65 |
if(walkSpeed == 0f) |
| 66 |
walkSpeed = 0.2f; |
| 67 |
break; |
| 68 |
|
| 69 |
case "RUN": |
| 70 |
walkSpeed = 0.5f; |
| 71 |
walkDirection = 0f; |
| 72 |
break; |
| 73 |
|
| 74 |
case "STOP": |
| 75 |
walkSpeed = 0f; |
| 76 |
walkDirection = 0f; |
| 77 |
break; |
| 78 |
|
| 79 |
case "JUMP": |
| 80 |
jumpNow = true; |
| 81 |
walkSpeed = 0.5f; |
| 82 |
walkDirection = 0f; |
| 83 |
break; |
| 84 |
|
| 85 |
case "HELLO": |
| 86 |
waveNow = true; |
| 87 |
walkSpeed = 0.0f; |
| 88 |
walkDirection = 0f; |
| 89 |
break; |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
void Start () |
| 97 |
{
|
| 98 |
// initialising reference variables |
| 99 |
anim = GetComponent<Animator>(); |
| 100 |
col = GetComponent<CapsuleCollider>(); |
| 101 |
|
| 102 |
if(anim.layerCount == 2) |
| 103 |
{
|
| 104 |
anim.SetLayerWeight(1, 1); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void FixedUpdate () |
| 110 |
{
|
| 111 |
anim.SetFloat("Speed", walkSpeed); // set our animator's float parameter 'Speed' equal to the vertical input axis
|
| 112 |
anim.SetFloat("Direction", walkDirection); // set our animator's float parameter 'Direction' equal to the horizontal input axis
|
| 113 |
anim.speed = animSpeed; // set the speed of our animator to the public variable 'animSpeed' |
| 114 |
//anim.SetLookAtWeight(lookWeight); // set the Look At Weight - amount to use look at IK vs using the head's animation |
| 115 |
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation |
| 116 |
|
| 117 |
if(anim.layerCount == 2) |
| 118 |
{
|
| 119 |
layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1); // set our layer2CurrentState variable to the current state of the second Layer (1) of animation |
| 120 |
} |
| 121 |
|
| 122 |
// if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the Jump bool parameter in the Animator to true |
| 123 |
if (currentBaseState.fullPathHash == locoState) |
| 124 |
{
|
| 125 |
if(jumpNow) |
| 126 |
{
|
| 127 |
jumpNow = false; |
| 128 |
anim.SetBool("Jump", true);
|
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// if we are in the jumping state... |
| 133 |
else if(currentBaseState.fullPathHash == jumpState) |
| 134 |
{
|
| 135 |
// ..and not still in transition.. |
| 136 |
if(!anim.IsInTransition(0)) |
| 137 |
{
|
| 138 |
if(useCurves) |
| 139 |
// ..set the collider height to a float curve in the clip called ColliderHeight |
| 140 |
col.height = anim.GetFloat("ColliderHeight");
|
| 141 |
|
| 142 |
// reset the Jump bool so we can jump again, and so that the state does not loop |
| 143 |
anim.SetBool("Jump", false);
|
| 144 |
} |
| 145 |
|
| 146 |
// Raycast down from the center of the character.. |
| 147 |
Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up); |
| 148 |
RaycastHit hitInfo = new RaycastHit(); |
| 149 |
|
| 150 |
if (Physics.Raycast(ray, out hitInfo)) |
| 151 |
{
|
| 152 |
// ..if distance to the ground is more than 1.75, use Match Target |
| 153 |
if (hitInfo.distance > 1.75f) |
| 154 |
{
|
| 155 |
|
| 156 |
// MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray. |
| 157 |
// Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5 |
| 158 |
// of the timeline of our animation clip |
| 159 |
anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// check if we are at idle, if so, let us Wave! |
| 165 |
else if (currentBaseState.fullPathHash == idleState) |
| 166 |
{
|
| 167 |
if(waveNow) |
| 168 |
{
|
| 169 |
waveNow = false; |
| 170 |
anim.SetBool("Wave", true);
|
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// if we enter the waving state, reset the bool to let us wave again in future |
| 175 |
if(layer2CurrentState.fullPathHash == waveState) |
| 176 |
{
|
| 177 |
anim.SetBool("Wave", false);
|
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
} |