回答編集履歴

1

キーボードによる入力について追記しました

2018/05/23 12:11

投稿

TakafumiYuasa
TakafumiYuasa

スコア124

test CHANGED
@@ -30,7 +30,11 @@
30
30
 
31
31
  {
32
32
 
33
- public Rigidbody rb ;
33
+ public Rigidbody rb;
34
+
35
+
36
+
37
+ private float speed = 0.5f;
34
38
 
35
39
 
36
40
 
@@ -48,9 +52,9 @@
48
52
 
49
53
  {
50
54
 
51
- float moveHorizontal = Input.GetAxis(Horizontal);
55
+ float moveHorizontal = Input.GetAxis("Horizontal");
52
56
 
53
- float moveVertical = Input.GetAxis(Vertical);
57
+ float moveVertical = Input.GetAxis("Vertical");
54
58
 
55
59
  Vector3 movement = new Vecter3(0.0f, 0.0f, -0.5f)
56
60
 
@@ -63,3 +67,89 @@
63
67
  }
64
68
 
65
69
  ```
70
+
71
+
72
+
73
+ キーボードで動かしたい場合は、以下のようにしてみてはどうでしょうか。
74
+
75
+
76
+
77
+ ```CSharp
78
+
79
+ public class players: MonoBehaviour
80
+
81
+ {
82
+
83
+ public Rigidbody rb;
84
+
85
+
86
+
87
+ private float speed = 0.5f;
88
+
89
+
90
+
91
+ void Start()
92
+
93
+ {
94
+
95
+ rb = GetComponent<Rigidbody>();
96
+
97
+ }
98
+
99
+
100
+
101
+ void Update()
102
+
103
+ {
104
+
105
+ float moveHorizontal = 0.0f;
106
+
107
+ float moveVertical = 0.0f;
108
+
109
+
110
+
111
+ if (Input.GetKey(KeyCode.UpArrow))
112
+
113
+ {
114
+
115
+ moveVertical = speed;
116
+
117
+ }
118
+
119
+ else if(Input.GetKey(KeyCode.DownArrow))
120
+
121
+ {
122
+
123
+ moveVertical = -speed;
124
+
125
+ }
126
+
127
+
128
+
129
+ if (Input.GetKey(KeyCode.RightArrow))
130
+
131
+ {
132
+
133
+ moveHorizontal = speed;
134
+
135
+ }
136
+
137
+ else if (Input.GetKey(KeyCode.LeftArrow))
138
+
139
+ {
140
+
141
+ moveHorizontal = -speed;
142
+
143
+ }
144
+
145
+
146
+
147
+ Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
148
+
149
+ rb.AddForce(movement);
150
+
151
+ }
152
+
153
+ }
154
+
155
+ ```