質問編集履歴
1
文法の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,116 @@
|
|
1
1
|
```C#
|
2
2
|
|
3
|
+
using UnityEngine;
|
4
|
+
|
5
|
+
using System.Collections;
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
public class PolisController : MonoBehaviour
|
10
|
+
|
11
|
+
{
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
CharacterController controller;
|
16
|
+
|
17
|
+
Animator animator;
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
Vector3 moveDirection = Vector3.zero; // 移動方向計算用
|
22
|
+
|
23
|
+
float gravity = 9.8f; // 重力
|
24
|
+
|
25
|
+
float speedZ = 3.0f; // 進行方向の速度
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
// Use this for initialization
|
30
|
+
|
31
|
+
void Start()
|
32
|
+
|
33
|
+
{
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
// 必要なコンポーネントを取得
|
38
|
+
|
39
|
+
controller = GetComponent<CharacterController>();
|
40
|
+
|
41
|
+
animator = GetComponent<Animator>();
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
// Update is called once per frame
|
48
|
+
|
49
|
+
void Update()
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
// Inputを検知して前に進める
|
56
|
+
|
57
|
+
if (Input.GetAxis("Vertical") > 0.0f)
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
moveDirection.z = Input.GetAxis("Vertical") * speedZ;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
else
|
66
|
+
|
67
|
+
{
|
68
|
+
|
69
|
+
moveDirection.z = 0;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// 方向転換
|
76
|
+
|
77
|
+
transform.Rotate(0, Input.GetAxis("Horizontal") * 3, 0);
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
// 重力分の力を毎フレーム追加
|
82
|
+
|
83
|
+
moveDirection.y -= gravity * Time.deltaTime;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
// 移動実行
|
90
|
+
|
91
|
+
Vector3 globalDirection = transform.TransformDirection(moveDirection);
|
92
|
+
|
93
|
+
controller.Move(globalDirection * Time.deltaTime);
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
// 移動後接地していたらY方向の速度はリセット
|
98
|
+
|
99
|
+
if (controller.isGrounded) moveDirection.y = 0.0f;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// 速度が0以上なら歩きフラグをtrueにする
|
106
|
+
|
3
|
-
animator.SetBool("walkFlag", moveDirection.z > 0.0f);
|
107
|
+
animator.SetBool("walkFlag", moveDirection.z > 0.0f);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
4
114
|
|
5
115
|
```
|
6
116
|
|
@@ -9,3 +119,7 @@
|
|
9
119
|
解決方法が分かる方、ご教授願います。
|
10
120
|
|
11
121
|
walkFlagをAnimatorでtrueにした後プレイすればアニメーションの再生はできているので、そこは問題ないです。
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
![イメージ説明](6c5d83d2faaf6f8dcf97b09cc22b00c7.png)
|