質問編集履歴
3
switch文の後に追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,12 +30,6 @@
|
|
30
30
|
|
31
31
|
```C#
|
32
32
|
|
33
|
-
//private Rigidbody2D body2d;
|
34
|
-
|
35
|
-
//public float speed = 2;
|
36
|
-
|
37
|
-
//---------------
|
38
|
-
|
39
33
|
using System.Collections;
|
40
34
|
|
41
35
|
using System.Collections.Generic;
|
@@ -82,6 +76,10 @@
|
|
82
76
|
|
83
77
|
|
84
78
|
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
85
83
|
void Start()
|
86
84
|
|
87
85
|
{
|
@@ -98,10 +96,20 @@
|
|
98
96
|
|
99
97
|
|
100
98
|
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
101
103
|
void Update()
|
102
104
|
|
103
105
|
{
|
104
106
|
|
107
|
+
|
108
|
+
|
109
|
+
Debug.Log(move);
|
110
|
+
|
111
|
+
|
112
|
+
|
105
113
|
if (savex == 0 && move == false)
|
106
114
|
|
107
115
|
{
|
@@ -134,21 +142,25 @@
|
|
134
142
|
|
135
143
|
}
|
136
144
|
|
137
|
-
|
145
|
+
|
138
146
|
|
139
147
|
if (move == true)
|
140
148
|
|
141
149
|
{
|
142
150
|
|
143
|
-
Vector2 direction = new Vector2(x, y).normalized;
|
151
|
+
Vector2 direction = new Vector2(savex, savey).normalized;
|
144
|
-
|
152
|
+
|
145
|
-
body2d.
|
153
|
+
body2d.velocity = direction * speed;
|
146
|
-
|
154
|
+
|
147
|
-
}
|
155
|
+
}
|
156
|
+
|
157
|
+
|
148
158
|
|
149
159
|
}
|
150
160
|
|
161
|
+
|
162
|
+
|
151
|
-
private void OnCollisionEnter2D(Collision2D col)
|
163
|
+
private void OnCollisionEnter2D(Collision2D col)
|
152
164
|
|
153
165
|
{
|
154
166
|
|
@@ -190,9 +202,15 @@
|
|
190
202
|
|
191
203
|
spriteRenderer.sprite = SpriteBlack;
|
192
204
|
|
205
|
+
move = false;
|
206
|
+
|
193
|
-
break;
|
207
|
+
break;
|
194
|
-
|
208
|
+
|
195
|
-
}
|
209
|
+
}
|
210
|
+
|
211
|
+
savex = 0;
|
212
|
+
|
213
|
+
savey = 0;
|
196
214
|
|
197
215
|
}
|
198
216
|
|
@@ -200,4 +218,8 @@
|
|
200
218
|
|
201
219
|
|
202
220
|
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
203
225
|
```
|
2
全文載せました。スクリプトはプレイヤーキャラクターにアタッチしています。
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,6 +16,18 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
|
20
|
+
|
21
|
+
追記:
|
22
|
+
|
23
|
+
教えていただいた部分を参考にして書かせて頂きました。
|
24
|
+
|
25
|
+
まっすぐ壁にぶつかるまで移動できるようになったのですが、その後全く動かなくなりました。
|
26
|
+
|
27
|
+
ご指摘お願いいたします。
|
28
|
+
|
29
|
+
|
30
|
+
|
19
31
|
```C#
|
20
32
|
|
21
33
|
//private Rigidbody2D body2d;
|
@@ -24,24 +36,168 @@
|
|
24
36
|
|
25
37
|
//---------------
|
26
38
|
|
39
|
+
using System.Collections;
|
40
|
+
|
41
|
+
using System.Collections.Generic;
|
42
|
+
|
43
|
+
using Unity.Mathematics;
|
44
|
+
|
45
|
+
using UnityEngine;
|
46
|
+
|
47
|
+
using UnityEngine.UI;
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
public class PlayerBox2 : MonoBehaviour
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
[SerializeField] private Sprite SpriteRed = default;
|
56
|
+
|
57
|
+
[SerializeField] private Sprite SpriteBlue = default;
|
58
|
+
|
59
|
+
[SerializeField] private Sprite SpriteGreen = default;
|
60
|
+
|
61
|
+
[SerializeField] private Sprite SpriteBlack = default;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
public float speed = 2;
|
66
|
+
|
67
|
+
private Rigidbody2D body2d;
|
68
|
+
|
69
|
+
private Animator anima;
|
70
|
+
|
71
|
+
private SpriteRenderer spriteRenderer;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
bool move = false;
|
76
|
+
|
77
|
+
float savex = 0;
|
78
|
+
|
79
|
+
float savey = 0;
|
80
|
+
|
81
|
+
float x, y;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
void Start()
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
//このスクリプトがアタッチされたゲームオブジェクトの動作をいただく
|
90
|
+
|
91
|
+
this.body2d = GetComponent<Rigidbody2D>();
|
92
|
+
|
93
|
+
this.anima = GetComponent<Animator>();
|
94
|
+
|
95
|
+
this.spriteRenderer = GetComponent<SpriteRenderer>();
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
27
101
|
void Update()
|
28
102
|
|
29
103
|
{
|
30
104
|
|
31
|
-
if(
|
105
|
+
if (savex == 0 && move == false)
|
32
|
-
|
106
|
+
|
33
|
-
{
|
107
|
+
{
|
34
|
-
|
108
|
+
|
35
|
-
|
109
|
+
x = Input.GetAxisRaw("Horizontal");
|
36
|
-
|
110
|
+
|
37
|
-
|
111
|
+
y = Input.GetAxisRaw("Vertical");
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
if (x != 0)
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
savex = x;
|
122
|
+
|
123
|
+
move = true;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
if (y != 0)
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
savey = y;
|
132
|
+
|
133
|
+
move = true;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
if (move == true)
|
140
|
+
|
141
|
+
{
|
38
142
|
|
39
143
|
Vector2 direction = new Vector2(x, y).normalized;
|
40
144
|
|
41
|
-
body2d.AddForce
|
145
|
+
body2d.AddForce(direction * speed); //body2d.addforce = の形では動かなかったので変更
|
42
146
|
|
43
147
|
}
|
44
148
|
|
45
149
|
}
|
46
150
|
|
151
|
+
private void OnCollisionEnter2D(Collision2D col)//移動先の壁の色にスプライトを変更し、moveをfalseにして次の移動に備える
|
152
|
+
|
153
|
+
{
|
154
|
+
|
155
|
+
switch (col.gameObject.tag)
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
case "blue":
|
160
|
+
|
161
|
+
spriteRenderer.sprite = SpriteBlue;
|
162
|
+
|
163
|
+
move = false;
|
164
|
+
|
165
|
+
break;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
case "red":
|
170
|
+
|
171
|
+
spriteRenderer.sprite = SpriteRed;
|
172
|
+
|
173
|
+
move = false;
|
174
|
+
|
175
|
+
break;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
case "green":
|
180
|
+
|
181
|
+
spriteRenderer.sprite = SpriteGreen;
|
182
|
+
|
183
|
+
move = false;
|
184
|
+
|
185
|
+
break;
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
case "black":
|
190
|
+
|
191
|
+
spriteRenderer.sprite = SpriteBlack;
|
192
|
+
|
193
|
+
break;
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
47
203
|
```
|
1
タグにC#を追加
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|