質問編集履歴
3
消去されていたものを元に戻しました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Unity,C#を使用して、キャラクターをグリッドベースで移動するスクリプトを作成したい
|
test
CHANGED
@@ -1 +1,93 @@
|
|
1
|
-
|
1
|
+
Unityで、ポケモンのようなグリッドベースで移動するスクリプトを作成しているのですが、行き詰まってしまったので今回投稿させていただきました。
|
2
|
+
|
3
|
+
ポケモンのゲームのような移動を実現したいため、1マス移動すると止まる・・・というものではなく、方向キーを入力している間は滑らかに動くことを目標としています。
|
4
|
+
|
5
|
+
以下のコードでは、キーを入力していない時に、Mathf.Lerpでpositionを整数にしているのですが、整数にしている間に他のキーを入力すると、他のキーの入力を優先してしまい、整数にしている作業は終わってしまいます。
|
6
|
+
|
7
|
+
positionを整数にする作業が、他のキーが入力されても妨害されなければ完成だと思うのですが、考えてみましたがわかりませんでした。
|
8
|
+
|
9
|
+
以下のコードでは修正するのが難しい場合は、丸々別のコードを教えていただきたいです。
|
10
|
+
|
11
|
+
見辛いコードだと思いますが、ご教授宜しくお願いします。
|
12
|
+
|
13
|
+
```C#
|
14
|
+
|
15
|
+
using System.Collections;
|
16
|
+
|
17
|
+
public class A : MonoBehaviour {
|
18
|
+
|
19
|
+
int button=0; //1=下 2=左 3=右 4=上
|
20
|
+
|
21
|
+
public int speed=6;
|
22
|
+
|
23
|
+
public int helpSpeed=10; // 整数のpositionにするために動かすスピード
|
24
|
+
|
25
|
+
//bool down =false;
|
26
|
+
|
27
|
+
public bool helpMoveFlag = false; // 整数のpositionに移動中はtrue
|
28
|
+
|
29
|
+
void Start () {
|
30
|
+
|
31
|
+
}
|
32
|
+
|
33
|
+
void Update () {
|
34
|
+
|
35
|
+
if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.UpArrow)
|
36
|
+
|
37
|
+
|| Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.LeftArrow)) {
|
38
|
+
|
39
|
+
if (Input.GetKey (KeyCode.LeftArrow)) {
|
40
|
+
|
41
|
+
transform.position += new Vector3 (-speed * Time.deltaTime, 0.0f, 0.0f);
|
42
|
+
|
43
|
+
button = 2;
|
44
|
+
|
45
|
+
} else if (Input.GetKey (KeyCode.RightArrow)) {
|
46
|
+
|
47
|
+
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
|
48
|
+
|
49
|
+
button = 3;
|
50
|
+
|
51
|
+
} else if (Input.GetKey (KeyCode.UpArrow)) {
|
52
|
+
|
53
|
+
transform.position += new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
|
54
|
+
|
55
|
+
button = 4;
|
56
|
+
|
57
|
+
} else if (Input.GetKey (KeyCode.DownArrow)) {
|
58
|
+
|
59
|
+
transform.position += new Vector3 (0.0f, -speed * Time.deltaTime, 0.0f);
|
60
|
+
|
61
|
+
button = 1;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
}else {
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
if (button == 1) {
|
70
|
+
|
71
|
+
transform.position = new Vector3 (transform.position.x,Mathf.Lerp(transform.position.y,Mathf.FloorToInt(transform.position.y),helpSpeed*Time.deltaTime), 0.0f);
|
72
|
+
|
73
|
+
} else if (button == 2) {
|
74
|
+
|
75
|
+
transform.position = new Vector3 ( Mathf.Lerp(transform.position.x,Mathf.FloorToInt(transform.position.x),helpSpeed*Time.deltaTime),transform.position.y, 0.0f);
|
76
|
+
|
77
|
+
} else if (button == 3) {
|
78
|
+
|
79
|
+
transform.position = new Vector3 ( Mathf.Lerp(transform.position.x,Mathf.CeilToInt(transform.position.x),helpSpeed*Time.deltaTime),transform.position.y ,0.0f);
|
80
|
+
|
81
|
+
} else if (button == 4) {
|
82
|
+
|
83
|
+
transform.position = new Vector3 (transform.position.x, Mathf.Lerp (transform.position.y, Mathf.CeilToInt (transform.position.y), helpSpeed * Time.deltaTime), 0.0f);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
```
|
2
修正依頼を受けたため編集中
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
編集。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
|
1
|
+
修正依頼を受けたため編集中。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
|
1
丸投げの指摘を受けたため編集
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
編集
|
test
CHANGED
@@ -1,135 +1 @@
|
|
1
|
-
Unityで、ポケモンのようなグリッドベースで移動するスクリプトを作成しているのですが、行き詰まってしまったので今回投稿させていただきました。
|
2
|
-
|
3
|
-
ポケモンのゲームのような移動を実現したいため、1マス移動すると止まる・・・というものではなく、方向キーを入力している間は滑らかに動くことを目標としています。
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
以下のコードでは、キーを入力していない時に、Mathf.Lerpでpositionを整数にしているのですが、整数にしている間に他のキーを入力すると、他のキーの入力を優先してしまい、整数にしている作業は終わってしまいます。
|
8
|
-
|
9
|
-
positionを整数にする作業が、他のキーが入力されても妨害されなければ完成だと思うのですが、考えてみましたがわかりませんでした。
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
以下のコードでは修正するのが難しい場合は、丸々別のコードを教えていただきたいです。
|
14
|
-
|
15
|
-
見辛いコードだと思いますが、ご教授宜しくお願いします。
|
16
|
-
|
17
|
-
```C#
|
18
|
-
|
19
|
-
using System.Collections;
|
20
|
-
|
21
|
-
public class A : MonoBehaviour {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
int button=0; //1=下 2=左 3=右 4=上
|
26
|
-
|
27
|
-
public int speed=6;
|
28
|
-
|
29
|
-
|
1
|
+
編集。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
|
30
|
-
|
31
|
-
//bool down =false;
|
32
|
-
|
33
|
-
public bool helpMoveFlag = false; // 整数のpositionに移動中はtrue
|
34
|
-
|
35
|
-
void Start () {
|
36
|
-
|
37
|
-
}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
void Update () {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.UpArrow)
|
48
|
-
|
49
|
-
|| Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.LeftArrow)) {
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
if (Input.GetKey (KeyCode.LeftArrow)) {
|
54
|
-
|
55
|
-
transform.position += new Vector3 (-speed * Time.deltaTime, 0.0f, 0.0f);
|
56
|
-
|
57
|
-
button = 2;
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
} else if (Input.GetKey (KeyCode.RightArrow)) {
|
62
|
-
|
63
|
-
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
|
64
|
-
|
65
|
-
button = 3;
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
} else if (Input.GetKey (KeyCode.UpArrow)) {
|
70
|
-
|
71
|
-
transform.position += new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
|
72
|
-
|
73
|
-
button = 4;
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
} else if (Input.GetKey (KeyCode.DownArrow)) {
|
78
|
-
|
79
|
-
transform.position += new Vector3 (0.0f, -speed * Time.deltaTime, 0.0f);
|
80
|
-
|
81
|
-
button = 1;
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
}else {
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
if (button == 1) {
|
90
|
-
|
91
|
-
transform.position = new Vector3 (transform.position.x,Mathf.Lerp(transform.position.y,Mathf.FloorToInt(transform.position.y),helpSpeed*Time.deltaTime), 0.0f);
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
} else if (button == 2) {
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
transform.position = new Vector3 ( Mathf.Lerp(transform.position.x,Mathf.FloorToInt(transform.position.x),helpSpeed*Time.deltaTime),transform.position.y, 0.0f);
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
} else if (button == 3) {
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
transform.position = new Vector3 ( Mathf.Lerp(transform.position.x,Mathf.CeilToInt(transform.position.x),helpSpeed*Time.deltaTime),transform.position.y ,0.0f);
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
} else if (button == 4) {
|
112
|
-
|
113
|
-
transform.position = new Vector3 (transform.position.x, Mathf.Lerp (transform.position.y, Mathf.CeilToInt (transform.position.y), helpSpeed * Time.deltaTime), 0.0f);
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
```
|