teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2021/08/13 01:35

投稿

fiveHundred
fiveHundred

スコア10457

answer CHANGED
@@ -5,4 +5,148 @@
5
5
  また、OnTriggerEnter()は3D用のものなので、2D用のもの(Rigidbody2Dなど)を使用する場合はOnTriggerEnter2D()でなければなりません。
6
6
 
7
7
  また、OnTriggerEnter()を使っているということはIs Triggerがオンにしているとは思いますが、その場合ぶつかってもすり抜けてしまいます。
8
- (スクリプトで制御しているのかもしれませんが、念のため)
8
+ (スクリプトで制御しているのかもしれませんが、念のため)
9
+
10
+ ---
11
+
12
+ 追記:
13
+
14
+ 変更箇所が多かったので、修正したコードを記載します。
15
+
16
+ ```C#
17
+ using System;
18
+ using System.Collections;
19
+ using System.Collections.Generic;
20
+ using UnityEngine;
21
+
22
+ public class PlayerIdou : MonoBehaviour
23
+ {
24
+ public float SpeedX;
25
+ public static int SpeedY = -65;
26
+ public float Muki;
27
+ float screenLeft = -2;//左端の座標x
28
+ float screenRight = 2;//右端の座標x
29
+
30
+ Rigidbody2D rigidbody2DComponent;
31
+ bool tapLeft = false;
32
+ bool tapRight = false;
33
+
34
+ void Start()
35
+ {
36
+ rigidbody2DComponent = GetComponent<Rigidbody2D>();
37
+ }
38
+
39
+ // Update is called once per frame
40
+ void Update()
41
+ {
42
+ if (Input.mousePosition.x >= Screen.width / 2)
43
+ {
44
+ if (Input.GetMouseButton(0))
45
+ {// 右側をタップしたら
46
+ tapRight = true;
47
+ }
48
+ }
49
+ else
50
+ {
51
+ if (Input.GetMouseButton(0))
52
+ {// 左側をタップしたら
53
+ tapLeft = true;
54
+ }
55
+ }
56
+ }
57
+
58
+ void FixedUpdate()
59
+ {
60
+ SpeedX = SpeedX * 0.7f;
61
+ Muki = Muki * 0.7f;
62
+ SpeedY += 5;
63
+
64
+ if (tapRight)
65
+ {// 右側をタップしたら
66
+ SpeedX += 0.1f;
67
+ Muki -= 1;
68
+ }
69
+ tapRight = false;
70
+
71
+ if (tapLeft)
72
+ {// 左側をタップしたら
73
+ SpeedX -= 0.1f;
74
+ Muki += 1;
75
+ }
76
+ tapLeft = false;
77
+
78
+ Vector3 pos = transform.position + new Vector3(SpeedX * 0.5f, 0, 0);
79
+ rigidbody2DComponent.MoveRotation(Quaternion.Euler(0, 0, Muki * 2));
80
+ pos.x = Mathf.Clamp(pos.x, screenLeft, screenRight);
81
+ rigidbody2DComponent.MovePosition(pos);
82
+ }
83
+
84
+ internal static float getSpeedY()
85
+ {
86
+ throw new NotImplementedException();
87
+ }
88
+
89
+ public static int GetSpeedY()
90
+ {
91
+ return SpeedY;
92
+ }
93
+
94
+ void OnCollisionEnter2D(Collision2D other)
95
+ {
96
+ //接触したオブジェクトのタグが"Ground"のとき
97
+ if (other.gameObject.CompareTag("Ground"))
98
+ {
99
+ SpeedY = -65;
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ ```C#
106
+ using System.Collections;
107
+ using System.Collections.Generic;
108
+ using UnityEngine;
109
+
110
+ public class GameGround : MonoBehaviour
111
+ {
112
+
113
+ public PlayerIdou playerIdou;
114
+ Rigidbody2D rigidbody2DComponent;
115
+
116
+ // Start is called before the first frame update
117
+ void Start()
118
+ {
119
+ rigidbody2DComponent = GetComponent<Rigidbody2D>();
120
+ }
121
+
122
+ void FixedUpdate()
123
+ {
124
+ float Jump;
125
+ Jump = PlayerIdou.SpeedY;
126
+ rigidbody2DComponent.MovePosition(transform.position + new Vector3(0, Jump * 0.01f, 0));
127
+ }
128
+ }
129
+ ```
130
+
131
+ また、Box Collider2DとRigidbody2Dを**両方**に付けて、Rigidbody2Dを以下のように設定しました。
132
+
133
+ - Body Type: Kinematic
134
+ - Use Full Kinematic Contacts: ON
135
+ - Collision Detection: Continuous(上記2つで上手くいかない場合)
136
+
137
+ 以下、変更した箇所についての説明です。
138
+
139
+ - OnTriggerEnter()からOnCollisionEnter2D()に変更しております。
140
+ Is Triggerを解除した場合は、OnTriggerEnter2D()では取得できないため、OnCollisionEnter2D()にしています。
141
+ - 物理判定や移動を行うものについては、Update()ではなくFixedUpdate()を使います。
142
+ ただし、操作入力の取得は逆にUpdate()でないといけないので、tapLeftやtapRightのように変数を仲介させるようにしましょう。
143
+ - 移動させる処理もtransformだと貫通することがあるので、Rigidbody2Dを使って移動させます。
144
+ 移動方法は色々ありますが、今回はMovePosition()やMoveRotation()を使っています。
145
+ ただし、これらはRigidbody2DがKinematicでないと正しく動作しないため、そのように設定しております。
146
+ また、オブジェクトの大きさや速度次第では、これでも貫通する可能性があるので、必要に応じてContinuousを設定する必要があります。
147
+
148
+ あと、変更はしていませんが、気になった点として以下があります。
149
+
150
+ - SpeedYがstaticですが、そうした意味が理解できませんでした。
151
+ むしろ、staticだと「Playerを2つ以上用意できない」「Playerを破棄した後もSpeedYの値が残ってしまう」という問題があります。
152
+ GameGroundにplayerIdouを用意してあるのだから、それにPlayerIdouをインスペクターで指定させればいい話だと思いますが、どうでしょう。