質問編集履歴

8

文章を修正

2022/03/25 01:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,13 +1,10 @@
1
- 提示コードのEnemy.cs のUpdate()部ですがなぜプレイヤーと自分との方向を取得してその方向に向けているにもかかわらず提示画像のようにバレットが横になってしまうしょうか? `プレイヤーの方向を弾の先が向くに回転させる方法が知りたいです`
1
+ 提示コードですが以下のように弾をプレイヤーの方向に向けているにもわらずが横を向いてしまう原因が知りたいす。`弾がプレイヤーの方向を向く`にはどすればいのしょうか?
2
2
 
3
- ※インスタンスプールを使っていてその都度座標を修正しいます。
4
- LookRotation: https://nekojara.city/unity-look-at#%E3%82%BF%E3%83%BC%E3%82%B2%E3%83%83%E3%83%88%E3%81%B8%E3%81%AE%E5%90%91%E3%81%8D%E3%81%AE%E8%A8%88%E7%AE%97
5
-
6
-
7
- このコードで自分方向の弾が回転しない原因がわかりません。
8
- ```
3
+ ```cs
9
4
  now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position);
10
5
  ```
6
+ の一行では向いてくれません。
7
+ LookRotation: https://nekojara.city/unity-look-at#%E3%82%BF%E3%83%BC%E3%82%B2%E3%83%83%E3%83%88%E3%81%B8%E3%81%AE%E5%90%91%E3%81%8D%E3%81%AE%E8%A8%88%E7%AE%97
11
8
 
12
9
 
13
10
 

7

文章を修正

2022/03/25 00:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/bfe977af-9472-4ceb-80ea-a3698aa84b65.png)
15
15
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/2f46f4a5-7052-4077-a6af-af3450d01a42.png)
16
-
16
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-25/177ea10b-1545-42d6-8367-4b984bfdaee9.png)
17
17
  ##### Bullet
18
18
  ```cs
19
19
  using System.Collections;

6

文章を修正

2022/03/25 00:08

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -13,6 +13,65 @@
13
13
 
14
14
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/bfe977af-9472-4ceb-80ea-a3698aa84b65.png)
15
15
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/2f46f4a5-7052-4077-a6af-af3450d01a42.png)
16
+
17
+ ##### Bullet
18
+ ```cs
19
+ using System.Collections;
20
+ using System.Collections.Generic;
21
+ using UnityEngine;
22
+
23
+ public class Enemy_Weapon : MonoBehaviour
24
+ {
25
+ [SerializeField] float speed;
26
+
27
+ private bool isShot = false;
28
+ private Vector3 vector;
29
+
30
+
31
+ public void Shot(Vector3 vec,Vector3 pos)
32
+ {
33
+ isShot = true;
34
+ vector = vec;
35
+
36
+ transform.position = pos;
37
+ }
38
+
39
+
40
+
41
+
42
+
43
+ // Start is called before the first frame update
44
+ void Start()
45
+ {
46
+
47
+ }
48
+
49
+ // Update is called once per frame
50
+ void Update()
51
+ {
52
+ if (isShot == true)
53
+ {
54
+ transform.Translate(vector * speed * Time.deltaTime);
55
+ }
56
+
57
+ }
58
+
59
+ public void OnTriggerEnter(Collider other)
60
+ {
61
+ if(other.gameObject.name == "Player")
62
+ {
63
+ transform.Find("Damage").GetComponent<ParticleSystem>().Play();
64
+ //transform.Find("Object").gameObject.SetActive(false);
65
+
66
+
67
+ }
68
+ }
69
+
70
+
71
+ }
72
+
73
+ ```
74
+
16
75
 
17
76
  ##### Enemy
18
77
  ```cs

5

文章を修正

2022/03/25 00:08

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 「Unity3D」敵の球をプレイヤーの方向に向けて発射する方法Quaternion.LookRotation()
1
+ 「Unity3D」Quaternion.LookRotation()が上手く機能しない原因が知りたい。
test CHANGED
@@ -41,7 +41,7 @@
41
41
  // Update is called once per frame
42
42
  void Update()
43
43
  {
44
- if(viewArea.GetComponent<ViewArea>().getIsInPlayer() == true)
44
+ if(viewArea.GetComponent<ViewArea>().getIsInPlayer() == true)
45
45
  {
46
46
  transform.rotation = Quaternion.LookRotation(Vector3.Normalize(viewArea.GetComponent<ViewArea>().getPlayerVector()),Vector3.up);
47
47
 
@@ -51,7 +51,9 @@
51
51
  Transform now = weapon.GetComponent<WeaponPool>().GetInstance();
52
52
 
53
53
  now.GetComponent<Enemy_Weapon>().Shot(viewArea.GetComponent<ViewArea>().getPlayerVector(), transform.position);
54
+ /////////////////////////////////////////////////////////////////
54
55
  now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position);
56
+ /////////////////////////////////////////////////////////////////
55
57
 
56
58
  time = 0;
57
59
  }
@@ -72,53 +74,8 @@
72
74
 
73
75
  ```
74
76
 
75
- ##### Weapon
76
- ```
77
- using System.Collections;
78
- using System.Collections.Generic;
79
- using UnityEngine;
80
-
81
- public class Enemy_Weapon : MonoBehaviour
82
- {
83
- [SerializeField] float speed;
84
-
85
- private bool isShot = false;
86
- private Vector3 vector;
87
-
88
-
89
- public void Shoot(Vector3 vec,Vector3 pos)
90
- {
91
- isShot = true;
92
- vector = vec;
93
-
94
- transform.position = pos;
95
- }
96
77
 
97
78
 
98
79
 
99
80
 
100
81
 
101
- // Start is called before the first frame update
102
- void Start()
103
- {
104
-
105
- }
106
-
107
- // Update is called once per frame
108
- void Update()
109
- {
110
- if (isShot == true)
111
- {
112
- transform.Translate(vector * speed * Time.deltaTime);
113
- }
114
-
115
- }
116
- }
117
-
118
- ```
119
-
120
-
121
-
122
-
123
-
124
-

4

文章を修正

2022/03/24 06:38

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ※インスタンスプールを使っていてその都度座標を修正しいます。
4
4
  LookRotation: https://nekojara.city/unity-look-at#%E3%82%BF%E3%83%BC%E3%82%B2%E3%83%83%E3%83%88%E3%81%B8%E3%81%AE%E5%90%91%E3%81%8D%E3%81%AE%E8%A8%88%E7%AE%97
5
+
6
+
7
+ このコードで自分方向の弾が回転しない原因がわかりません。
8
+ ```
9
+ now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position);
10
+ ```
11
+
12
+
5
13
 
6
14
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/bfe977af-9472-4ceb-80ea-a3698aa84b65.png)
7
15
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/2f46f4a5-7052-4077-a6af-af3450d01a42.png)

3

文章を修正

2022/03/24 06:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,7 @@
1
1
  提示コードのEnemy.cs のUpdate()部ですがなぜプレイヤーと自分との方向を取得してその方向に向けているのにもかかわらず提示画像のようにバレットが横になってしまうのでしょうか? `プレイヤーの方向を弾の先が向くように回転させる方法が知りたいです`
2
2
 
3
3
  ※インスタンスプールを使っていてその都度座標を修正しいます。
4
+ LookRotation: https://nekojara.city/unity-look-at#%E3%82%BF%E3%83%BC%E3%82%B2%E3%83%83%E3%83%88%E3%81%B8%E3%81%AE%E5%90%91%E3%81%8D%E3%81%AE%E8%A8%88%E7%AE%97
4
5
 
5
6
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/bfe977af-9472-4ceb-80ea-a3698aa84b65.png)
6
7
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/2f46f4a5-7052-4077-a6af-af3450d01a42.png)
@@ -32,18 +33,18 @@
32
33
  // Update is called once per frame
33
34
  void Update()
34
35
  {
35
- if(viewArea.GetComponent<ViewArea>().getIsInPlayer() == true)
36
+ if(viewArea.GetComponent<ViewArea>().getIsInPlayer() == true)
36
37
  {
37
- transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerVector(),Vector3.up);
38
+ transform.rotation = Quaternion.LookRotation(Vector3.Normalize(viewArea.GetComponent<ViewArea>().getPlayerVector()),Vector3.up);
38
39
 
39
40
  time += Time.deltaTime;
40
41
  if(time > 1)
41
42
  {
42
43
  Transform now = weapon.GetComponent<WeaponPool>().GetInstance();
43
- ////////////////////////////////
44
+
44
- now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position, Vector3.up);
45
- ///////////////////////////////
46
- now.GetComponent<Enemy_Weapon>().Shoot(viewArea.GetComponent<ViewArea>().getPlayerVector(), transform.position);
45
+ now.GetComponent<Enemy_Weapon>().Shot(viewArea.GetComponent<ViewArea>().getPlayerVector(), transform.position);
46
+ now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position);
47
+
47
48
  time = 0;
48
49
  }
49
50
  }

2

文章を修正

2022/03/24 05:39

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 提示コードのEnemy.cs のUpdate()部ですがなぜプレイヤーと自分との方向を取得してその方向に向けているのにもかかわらず提示画像のようにバレットが横になってしまうのでしょうか? 
1
+ 提示コードのEnemy.cs のUpdate()部ですがなぜプレイヤーと自分との方向を取得してその方向に向けているのにもかかわらず提示画像のようにバレットが横になってしまうのでしょうか? `プレイヤーの方向を弾の先が向くように回転させる方法が知りたいです`
2
2
 
3
3
  ※インスタンスプールを使っていてその都度座標を修正しいます。
4
4
 

1

文章を修正

2022/03/24 05:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,9 @@
1
- 提示コードのEnemy.cs のUpdate()部ですがなぜプレイヤーと自分との方向を取得してその方向に向けているのにもかかわらず提示画像のようにバレットが横になってしまうのでしょうか?
1
+ 提示コードのEnemy.cs のUpdate()部ですがなぜプレイヤーと自分との方向を取得してその方向に向けているのにもかかわらず提示画像のようにバレットが横になってしまうのでしょうか? 
2
+
3
+ ※インスタンスプールを使っていてその都度座標を修正しいます。
2
4
 
3
5
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/bfe977af-9472-4ceb-80ea-a3698aa84b65.png)
6
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-03-24/2f46f4a5-7052-4077-a6af-af3450d01a42.png)
4
7
 
5
8
  ##### Enemy
6
9
  ```cs