質問するログイン新規登録

質問編集履歴

1

モデルの挙動に関するScriptを追加しました。

2018/12/10 12:05

投稿

nishi835
nishi835

スコア15

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,254 @@
10
10
 
11
11
  Unityのバージョンは2018.2.9f1 Personalです。
12
12
 
13
- ![イメージ説明](258d9f8b1146b299752fa629e22a05d4.png)
13
+ ![イメージ説明](258d9f8b1146b299752fa629e22a05d4.png)
14
+
15
+ 少し長いですが、3Dモデルを動かしているスクリプトの関係のありそうな部分を添付いたします。
16
+
17
+ ```C#
18
+ void Update()
19
+ {
20
+ var cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; // カメラが追従するための動作
21
+ Vector3 direction = Vector3.zero;
22
+
23
+ // 地面に接地している時
24
+ if (CheckGrounded())
25
+ {
26
+ if (Input.GetAxis("Vertical01") == 0 && Input.GetAxis("Horizontal01") == 0) // テンキーや3Dスティックの入力(GetAxis)がゼロの時の動作
27
+ {
28
+ animCon.SetBool("Walk", false);
29
+ animCon.SetBool("Run", false);
30
+ }
31
+ else if (!(Input.GetButton("SideRight") || Input.GetButton("SideLeft")) && !(animCon.GetBool("FrontFlip") || animCon.GetBool("BackFlip")))
32
+ {
33
+ if (Input.GetButton("Dash"))
34
+ {
35
+ animCon.SetBool("Run", true);
36
+ dashTime += Time.deltaTime;
37
+
38
+ endurance -= consRate * Time.deltaTime;
39
+
40
+ if (Input.GetButtonDown("Slide"))
41
+ {
42
+ animCon.SetBool("Slide", true);
43
+ }
44
+ }
45
+ else
46
+ {
47
+ animCon.SetBool("Run", false);
48
+ animCon.SetBool("Walk", true);
49
+ dashTime = 0.0f;
50
+ }
51
+
52
+ // フリップ
53
+ if (Input.GetAxis("Flip") > 0)
54
+ {
55
+ animCon.SetBool("FrontFlip", true);
56
+ }
57
+ else if (Input.GetAxis("Flip") < 0)
58
+ {
59
+ animCon.SetBool("BackFlip", true);
60
+ }
61
+
62
+ direction = cameraForward * Input.GetAxis("Vertical01") + Camera.main.transform.right * Input.GetAxis("Horizontal01"); // テンキーや3Dスティックの入力(GetAxis)があるとdirectionに値を返す
63
+ Turn(direction); // 向きを変える動作の処理を実行する(後述)
64
+ }
65
+
66
+ moveDirection = direction * moveSpeed; //移動スピードを向いている方向に与える
67
+ moveDirection.y = 0f; //Y方向への速度をゼロにする
68
+
69
+ if (isWallJump)
70
+ {
71
+ animCon.SetBool("Idle", true);
72
+ isWallJump = false;
73
+ }
74
+
75
+ // 着地したらアニメーションパラメータのFallをfalseにする
76
+ animCon.SetBool("Fall", false);
77
+ animCon.SetBool("Jump", false);
78
+ animCon.SetBool("WallJump", false);
79
+
80
+ var input = new Vector3(Input.GetAxis("Horizontal01"), 0f, Input.GetAxis("Vertical01"));
81
+
82
+ if (input.magnitude > 0f)
83
+ {
84
+ // 着地後移動キーを押したら着地アニメーション終了
85
+ animCon.SetBool("Landing", false);
86
+ }
87
+
88
+ var stateInfo = animCon.GetCurrentAnimatorStateInfo(0);
89
+
90
+ // ジャンプ
91
+ if (Input.GetButtonDown("Jump") && !(Input.GetButton("SideRight") || Input.GetButton("SideLeft")) && !(animCon.GetBool("FrontFlip") || animCon.GetBool("BackFlip") || stateInfo.IsName("Roll")))
92
+ {
93
+ rb.AddForce(0, jumpPower, 0, ForceMode.Impulse);
94
+ isJump = true;
95
+ }
96
+
97
+ // ジャンプ力リセット
98
+ jumpPower = defaultJumpPower;
99
+ }
100
+ else // 空中なら
101
+ {
102
+ // ジャンプアクション後、空中に移動したらジャンプアニメーションを始める
103
+ if (isJump)
104
+ {
105
+ animCon.SetBool("Jump", true);
106
+ isJump = false;
107
+ }
108
+
109
+ //animCon.SetBool("isGrounded", false);
110
+ animCon.SetBool("Idle", false);
111
+
112
+ if (!animCon.GetBool("Fall")) // アニメーションパラメータFallがfalseの時で地面との距離が遠かったらFallをtrueにする
113
+ {
114
+ if (!Physics.SphereCast(new Ray(spine.position, Vector3.down), 0.5f, distanceToTheGround, LayerMask.GetMask("Field")))
115
+ {
116
+ animCon.SetBool("Fall", true);
117
+ }
118
+ }
119
+ else if (animCon.GetBool("Fall")) // 落下アニメーションの時はレイを飛ばし着地アニメーションにする
120
+ {
121
+ if (Physics.Linecast(spine.position, spine.position + Vector3.down * distanceToLanding, LayerMask.GetMask("Field")) && !isRoll)
122
+ {
123
+ animCon.SetBool("Landing", true);
124
+ }
125
+ }
126
+
127
+ if (Input.GetButtonDown("Jump"))
128
+ {
129
+ isRoll = true;
130
+ animCon.SetBool("Landing", false);
131
+ }
132
+
133
+ jumpPower += gravity * Time.deltaTime; // 時間経過とともに壁ジャンプ力を増す
134
+ }
135
+
136
+ if (Input.GetButtonDown("Action"))
137
+ {
138
+ if (rader.CanHang())
139
+ {
140
+ //Rayの作成       ↓Rayを飛ばす原点   ↓Rayを飛ばす方向
141
+ Ray ray = new Ray(transform.position, new Vector3(0, -1, 0));
142
+
143
+ //Rayが当たったオブジェクトの情報を入れる箱
144
+ RaycastHit hit;
145
+
146
+ //Rayの飛ばせる距離
147
+ int distance = 10;
148
+ Physics.Raycast(ray, out hit, distance);
149
+
150
+ float h_angle = hit.transform.eulerAngles.y;
151
+ float p_angle = transform.eulerAngles.y;
152
+
153
+ for (int i = 0; i < 4; i++)
154
+ {
155
+ h_angle -= 90 * i;
156
+ if (h_angle < 0) { h_angle = 360 + h_angle; }
157
+ float diff = Mathf.Abs(h_angle - p_angle);
158
+ if (diff > 180) { diff = 360 - diff; }
159
+ if (diff <= 45)
160
+ {
161
+ transform.rotation = Quaternion.Euler(0.0f, h_angle, 0.0f);
162
+ break;
163
+ }
164
+ }
165
+
166
+ animCon.SetBool("HangWall", true);
167
+
168
+ StartCoroutine(DelayMethod(0.65f, () =>
169
+ {
170
+ transform.position = Vector3.Lerp(transform.position, transform.position + new Vector3(0.0f, 0.0f, -0.15f), 1.0f);
171
+ }));
172
+ }
173
+ }
174
+
175
+ if (animCon.GetBool("HangWall"))
176
+ {
177
+ Physics.gravity = new Vector3(0.0f, 0.0f, 0.0f);
178
+ }
179
+ else
180
+ {
181
+ Physics.gravity = new Vector3(0.0f, defaultGravity, 0.0f);
182
+ }
183
+
184
+ switch(animClip)
185
+ {
186
+ case AnimClip.GetHit:
187
+ if(animCon.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f)
188
+ {
189
+ animClip = AnimClip.Idle;
190
+ animCon.CrossFadeInFixedTime(AnimClip.Idle.ToString(), 0.5f);
191
+ }
192
+ break;
193
+ }
194
+
195
+ // 移動
196
+ //charaCon.Move(moveDirection * Time.deltaTime); //CharacterControllerの付いているこのオブジェクトを移動させる処理
197
+ rb.AddForce(moveDirection.x * moveSpeed, 0, moveDirection.z * moveSpeed);
198
+ }
199
+
200
+ // 汎用コルーチン
201
+ private IEnumerator DelayMethod(float waitTime, Action action)
202
+ {
203
+ yield return new WaitForSeconds(waitTime);
204
+ action();
205
+ }
206
+
207
+ // 向きを変える動作の処理
208
+ void Turn(Vector3 direction)
209
+ {
210
+ Quaternion q = Quaternion.LookRotation(direction); // 向きたい方角をQuaternion型に直す
211
+ transform.rotation = Quaternion.RotateTowards(transform.rotation, q, turnSpeed * Time.deltaTime); // 向きを q に向けてじわ~っと変化させる.
212
+ }
213
+
214
+ // スタミナを返す
215
+ public int Endurance()
216
+ {
217
+ return (int)endurance;
218
+ }
219
+
220
+ // 着地判定
221
+ public bool CheckGrounded()
222
+ {
223
+ //CharacterControlle.IsGroundedがtrueならRaycastを使わずに判定終了
224
+ //if (charaCon.isGrounded) { return true; }
225
+
226
+ //放つ光線の初期位置と姿勢
227
+ //若干身体にめり込ませた位置から発射しないと正しく判定できない時がある
228
+ var ray = new Ray(this.transform.position + new Vector3(0.0f, 0.1f, -0.1f), Vector3.down);
229
+
230
+ //探索距離
231
+ var tolerance = 0.3f;
232
+
233
+ //Raycastがhitするかどうかで判定
234
+ //地面にのみ衝突するようにレイヤを指定する
235
+ return Physics.Raycast(ray, tolerance, LayerMask.GetMask("Field"));
236
+ }
237
+
238
+ // 壁ジャンプ
239
+ private void OnControllerColliderHit(ControllerColliderHit hit)
240
+ {
241
+ if (!CheckGrounded())
242
+ //if (!charaCon.isGrounded)
243
+ {
244
+ if (hit.normal.y < 0.1f)
245
+ {
246
+ if (Input.GetButtonDown("Jump"))
247
+ {
248
+ animCon.SetBool("WallJump", true);
249
+ isWallJump = true;
250
+
251
+ float angle = Mathf.Atan2(hit.normal.x, hit.normal.z) * Mathf.Rad2Deg;
252
+ transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
253
+ moveDirection.x = hit.normal.x * moveSpeed;
254
+ moveDirection.z = hit.normal.z * moveSpeed;
255
+ moveDirection.y = jumpPower;
256
+ }
257
+ }
258
+ }
259
+ }
260
+
261
+ }
262
+
263
+ ```