回答編集履歴
1
キーボードによる入力について追記しました
answer
CHANGED
@@ -14,8 +14,10 @@
|
|
14
14
|
```CSharp
|
15
15
|
public class players: MonoBehaviour
|
16
16
|
{
|
17
|
-
public Rigidbody rb
|
17
|
+
public Rigidbody rb;
|
18
18
|
|
19
|
+
private float speed = 0.5f;
|
20
|
+
|
19
21
|
void Start()
|
20
22
|
{
|
21
23
|
rb = GetComponent<Rigidbody>();
|
@@ -23,11 +25,54 @@
|
|
23
25
|
|
24
26
|
void Update()
|
25
27
|
{
|
26
|
-
float moveHorizontal = Input.GetAxis(
|
28
|
+
float moveHorizontal = Input.GetAxis("Horizontal");
|
27
|
-
float moveVertical = Input.GetAxis(
|
29
|
+
float moveVertical = Input.GetAxis("Vertical");
|
28
30
|
Vector3 movement = new Vecter3(0.0f, 0.0f, -0.5f)
|
29
31
|
|
30
32
|
rb.AddForce(movement * speed);
|
31
33
|
}
|
32
34
|
}
|
35
|
+
```
|
36
|
+
|
37
|
+
キーボードで動かしたい場合は、以下のようにしてみてはどうでしょうか。
|
38
|
+
|
39
|
+
```CSharp
|
40
|
+
public class players: MonoBehaviour
|
41
|
+
{
|
42
|
+
public Rigidbody rb;
|
43
|
+
|
44
|
+
private float speed = 0.5f;
|
45
|
+
|
46
|
+
void Start()
|
47
|
+
{
|
48
|
+
rb = GetComponent<Rigidbody>();
|
49
|
+
}
|
50
|
+
|
51
|
+
void Update()
|
52
|
+
{
|
53
|
+
float moveHorizontal = 0.0f;
|
54
|
+
float moveVertical = 0.0f;
|
55
|
+
|
56
|
+
if (Input.GetKey(KeyCode.UpArrow))
|
57
|
+
{
|
58
|
+
moveVertical = speed;
|
59
|
+
}
|
60
|
+
else if(Input.GetKey(KeyCode.DownArrow))
|
61
|
+
{
|
62
|
+
moveVertical = -speed;
|
63
|
+
}
|
64
|
+
|
65
|
+
if (Input.GetKey(KeyCode.RightArrow))
|
66
|
+
{
|
67
|
+
moveHorizontal = speed;
|
68
|
+
}
|
69
|
+
else if (Input.GetKey(KeyCode.LeftArrow))
|
70
|
+
{
|
71
|
+
moveHorizontal = -speed;
|
72
|
+
}
|
73
|
+
|
74
|
+
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
|
75
|
+
rb.AddForce(movement);
|
76
|
+
}
|
77
|
+
}
|
33
78
|
```
|