質問編集履歴

2

Script

2022/07/29 07:04

投稿

TigerW
TigerW

スコア0

test CHANGED
File without changes
test CHANGED
@@ -16,12 +16,77 @@
16
16
  ### 該当のソースコード
17
17
 
18
18
  ```ここに言語名を入力
19
+ using System.Collections;
20
+ using System.Collections.Generic;
21
+ using UnityEngine;
22
+
23
+ public class RocketDestroyer : MonoBehaviour
24
+ {
25
+ public GameObject Rocket1;
26
+ public GameObject Rocket2;
27
+ public GameObject RocketPlace1;
28
+ public GameObject RocketPlace2;
29
+ float speed;
30
+ private Transform player;
31
+ private Camera mainCamera;
32
+ public Vector3 hoge;
33
+
34
+ private Vector3 currentPosition = Vector3.zero;
35
+
36
+ void Start()
37
+ {
38
+ speed = 30.0f; // 弾の速度
39
+ player = GameObject.FindGameObjectWithTag("Player").transform;
40
+ mainCamera = Camera.main;
41
+ Rocket1.transform.localPosition = new Vector3(6.5f, 1.51f, -10);
42
+ Debug.Log("ROCKET!!!");
43
+ Rocket2.transform.localPosition = new Vector3(-8, 1.51f, -10);
44
+ }
45
+
46
+ void Update()
47
+ {
48
+
49
+ if (Input.GetMouseButton(0))
50
+ {
51
+ var distance = Vector3.Distance(player.transform.position, mainCamera.transform.position);
52
+ //var これはfloat var とはかんたんに変数を表したもの(自動的に)
53
+ var mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
54
+ //var Vector3
55
+
56
+ currentPosition = mainCamera.ScreenToWorldPoint(mousePosition);
57
+ }
58
+
59
+ if (Input.GetMouseButtonDown(0))
60
+ {
61
+
62
+ // 弾(ゲームオブジェクト)の生成
63
+ GameObject clone1 = Instantiate(Rocket1, transform.position, Quaternion.identity);
64
+ GameObject clone2 = Instantiate(Rocket2, transform.position, Quaternion.identity);
65
+
66
+ // クリックした座標の取得(スクリーン座標からワールド座標に変換)
67
+ Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
68
+
69
+ // 向きの生成(Z成分の除去と正規化)
70
+ Vector3 shotForward = Vector3.Scale((mouseWorldPos - transform.position), new Vector3(1, 1, 0)).normalized;
71
+
72
+ // 弾に速度を与える
73
+ clone1.GetComponent<Rigidbody2D>().velocity = shotForward * speed;
74
+ clone2.GetComponent<Rigidbody2D>().velocity = shotForward * speed;
75
+
76
+ if (Input.GetMouseButtonDown(0))
77
+ {
78
+ transform.SetParent(null);
79
+ Debug.Log("transform.SetParent(null)");
80
+ }
81
+
82
+ }
83
+ }
84
+
19
- private void OnCollisionEnter(Collision collision)
85
+ private void OnCollisionEnter(Collision collision)
20
86
  {
21
87
  if (collision.gameObject.tag == "Rocket Position")
22
88
  {
23
89
  transform.SetParent(collision.transform);
24
- Debug.Log("Parent")
25
90
  }
26
91
  if (collision.gameObject.tag == "Target")
27
92
  {
@@ -29,6 +94,16 @@
29
94
  Rocket2.transform.localPosition = new Vector3(-29.6f, 1.51f, 19.88f);
30
95
  }
31
96
  }
97
+ void OnDrawGizmos()
98
+ {
99
+ if (currentPosition != Vector3.zero)
100
+ {
101
+ Gizmos.color = Color.blue;
102
+ Gizmos.DrawSphere(currentPosition, 0.5f);
103
+ }
104
+ }
105
+ }
106
+
32
107
  ```
33
108
 
34
109
  ### 試したこと

1

タイトル

2022/07/29 07:01

投稿

TigerW
TigerW

スコア0

test CHANGED
@@ -1 +1 @@
1
- ロケットを飛ばす機能 transform.SetParent(collision.transform)
1
+ Unity C# ロケットを飛ばす機能 transform.SetParent(collision.transform)
test CHANGED
File without changes