質問編集履歴

1

試したこと、補足情報の追加 インスペクターの画像の追加

2022/11/01 02:33

投稿

Bis1335
Bis1335

スコア3

test CHANGED
File without changes
test CHANGED
@@ -19,7 +19,37 @@
19
19
  ### 該当のソースコード
20
20
 
21
21
  ```C♯
22
+ using System.Collections;
23
+ using System.Collections.Generic;
24
+ using UnityEngine;
25
+
26
+ public class PlayerController: MonoBehaviour
27
+ {
28
+ public Rigidbody2D rigid2D;
29
+
30
+
31
+
32
+ // Start is called before the first frame update
33
+ void Start()
34
+ {
35
+ Application.targetFrameRate=60;
36
+ this.rigid2D=GetComponent<Rigidbody2D>();
37
+ }
38
+
39
+ // Update is called once per frame
40
+ void Update()
41
+ {
42
+ if(Input.GetKeyDown(KeyCode.Space))
43
+ {
44
+ this.rigid2D.velocity=new Vector2(0,5f);
45
+
46
+ if(transform.position.x!=3)
47
+ {
48
+ this.rigid2D.velocity=new Vector2(0,-5f);
49
+ }
50
+ }
51
+
22
- if(Input.GetKeyDown(KeyCode.Return))
52
+ if(Input.GetKeyDown(KeyCode.Return))
23
53
  {
24
54
  this.rigid2D.velocity*=0;
25
55
  void OnTriggerStay2D(Collider2D other)
@@ -29,7 +59,30 @@
29
59
  Debug.Log("HIT!");
30
60
  }
31
61
  }
62
+
63
+
32
- }
64
+ }
65
+
66
+
67
+ }
68
+
69
+ void OnTriggerEnter2D(Collider2D other)
70
+ {
71
+ if(other.CompareTag("Wall"))
72
+ {
73
+ this.rigid2D.velocity*=-1;
74
+ }
75
+ }
76
+ }
33
77
  ```
34
78
 
79
+ ### 試したこと
80
+ void OnTriggerEnter2Dの部分をif文の外に出してみましたが、そうすると意図した動きになりません
81
+ (本来はエンターキーで的を停止させた後だけに”HIT!”と表示させたいのに、void OnTriggerEnter2Dの部分をif文の外に出すと、的が動いていても点線を通り越すたびに”HIT!”と表示される)
35
82
 
83
+ ### 補足情報
84
+ プロジェクトのエディターナンバーは 2021.3.11f1で、的のインスペクターは次の通りです
85
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-01/911e0368-b882-448f-b19c-4a4efaa492d0.png)
86
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-01/6f6d3c8b-d174-4455-8540-2392bd66f4f5.png)
87
+
88
+