-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdleChanger.cs
107 lines (87 loc) · 3.22 KB
/
IdleChanger.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
using UnityEngine;
using System.Collections;
//
// ↑↓キーでループアニメーションを切り替えるスクリプト(ランダム切り替え付き)Ver.3
// 2014/04/03 N.Kobayashi
//
// Require these components when using this script
[RequireComponent(typeof(Animator))]
public class IdleChanger : MonoBehaviour
{
private Animator anim; // Animatorへの参照
private AnimatorStateInfo currentState; // 現在のステート状態を保存する参照
private AnimatorStateInfo previousState; // ひとつ前のステート状態を保存する参照
public bool _random = false; // ランダム判定スタートスイッチ
public float _threshold = 0.5f; // ランダム判定の閾値
public float _interval = 2f; // ランダム判定のインターバル
//private float _seed = 0.0f; // ランダム判定用シード
// Use this for initialization
void Start ()
{
// 各参照の初期化
anim = GetComponent<Animator> ();
currentState = anim.GetCurrentAnimatorStateInfo (0);
previousState = currentState;
// ランダム判定用関数をスタートする
StartCoroutine ("RandomChange");
}
// Update is called once per frame
void Update ()
{
// ↑キー/スペースが押されたら、ステートを次に送る処理
if (Input.GetKeyDown ("up") || Input.GetButton ("Jump")) {
// ブーリアンNextをtrueにする
anim.SetBool ("Next", true);
}
// ↓キーが押されたら、ステートを前に戻す処理
if (Input.GetKeyDown ("down")) {
// ブーリアンBackをtrueにする
anim.SetBool ("Back", true);
}
// "Next"フラグがtrueの時の処理
if (anim.GetBool ("Next")) {
// 現在のステートをチェックし、ステート名が違っていたらブーリアンをfalseに戻す
currentState = anim.GetCurrentAnimatorStateInfo (0);
if (previousState.nameHash != currentState.nameHash) {
anim.SetBool ("Next", false);
previousState = currentState;
}
}
// "Back"フラグがtrueの時の処理
if (anim.GetBool ("Back")) {
// 現在のステートをチェックし、ステート名が違っていたらブーリアンをfalseに戻す
currentState = anim.GetCurrentAnimatorStateInfo (0);
if (previousState.nameHash != currentState.nameHash) {
anim.SetBool ("Back", false);
previousState = currentState;
}
}
}
void OnGUI()
{
GUI.Box(new Rect(Screen.width - 110 , 10 ,100 ,90), "Change Motion");
if(GUI.Button(new Rect(Screen.width - 100 , 40 ,80, 20), "Next"))
anim.SetBool ("Next", true);
if(GUI.Button(new Rect(Screen.width - 100 , 70 ,80, 20), "Back"))
anim.SetBool ("Back", true);
}
// ランダム判定用関数
IEnumerator RandomChange ()
{
// 無限ループ開始
while (true) {
//ランダム判定スイッチオンの場合
if (_random) {
// ランダムシードを取り出し、その大きさによってフラグ設定をする
float _seed = Random.Range (-1f, 1f);
if (_seed <= -_threshold) {
anim.SetBool ("Back", true);
} else if (_seed >= _threshold) {
anim.SetBool ("Next", true);
}
}
// 次の判定までインターバルを置く
yield return new WaitForSeconds (_interval);
}
}
}