wiiのタンクのような、戦車で戦うゲームを作っています。
そのなかで、プレイヤーが見えた瞬間に敵に球を発射してもらいたく、普通の球とは別に見えない球を高速でプレイヤーの座標に発射し、それがヒットしたら普通の球を発射するというふうにしようと考えました。
なのでまず
void OnCollisionEnter2D(Collision2D collision){ Debug.Log("HitTest"); 普通の球を発射する関数; Destroy(this.gameObject);
としてみましたが、普通の球を発射する関数が void update 内にある為、呼び出すことができませんでした。void updateの中でstaticやpublicがつけれないのでこの方法は一度諦めました(できるならご教授ください)
その次に、衝突時に変数を代入して、update内のif文でその変数を感知したら普通の球を発射するものを考えました
void OnCollisionEnter2D(Collision2D collision){ Debug.Log("HitTest"); AttackTest =1; Destroy(this.gameObject); } void update(){ void EnemyBall(){ // 弾(ゲームオブジェクト)の生成 GameObject clone = Instantiate(ball, transform.position, Quaternion.identity); // クリックした座標の取得(スクリーン座標からワールド座標に変換) Vector3 mouseWorldPos = tmp; // 向きの生成(Z成分の除去と正規化) Vector3 shotForward = Vector3.Scale((mouseWorldPos - transform.position), new Vector3(1, 1, 0)).normalized; // 弾に速度を与える clone.GetComponent<Rigidbody2D>().velocity = shotForward * speed; } if(AttackTest == 1){ EnemyBall(); AttackTest = 0; } }
しかしこれだと、なんか反応しません。おそらくAttackTestに数を代入しているのが反映されていません。
どうすれば解決できそうでしょうか、 よろしくお願いします。
最後に(試行錯誤中でぐちゃぐちゃですが)コード全体を貼ります
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestBall : MonoBehaviour { public GameObject testtestBall; GameObject gameobject; float speed2; public float span2 = 0.5f; private float testtime = 0f; public GameObject ball; float speed; public int AttackTest; void OnCollisionEnter2D(Collision2D collision){ Debug.Log("HitTest"); AttackTest =1; Destroy(this.gameObject); } // Start is called before the first frame update void Start() { speed = 4.0f; speed2 = 100.0f; gameobject = GameObject.Find("Enemy"); AttackTest = 0; } // Update is called once per frame void Update() { Vector3 tmp = GameObject.Find("HeavyHullD").transform.position; testtime += Time.deltaTime; if(testtime > span2){ testBall(); testtime = 0f; } void testBall(){ GameObject clone = Instantiate(testtestBall, transform.position, Quaternion.identity); Vector3 mouseWorldPos2 = tmp; Vector3 shotForwards = Vector3.Scale((mouseWorldPos2 - transform.position), new Vector3(1, 1, 0)).normalized; clone.GetComponent<Rigidbody2D>().velocity = shotForwards * speed2; } void EnemyBall(){ // 弾(ゲームオブジェクト)の生成 GameObject clone = Instantiate(ball, transform.position, Quaternion.identity); // クリックした座標の取得(スクリーン座標からワールド座標に変換) Vector3 mouseWorldPos = tmp; // 向きの生成(Z成分の除去と正規化) Vector3 shotForward = Vector3.Scale((mouseWorldPos - transform.position), new Vector3(1, 1, 0)).normalized; // 弾に速度を与える clone.GetComponent<Rigidbody2D>().velocity = shotForward * speed; } if(AttackTest == 1){ EnemyBall(); AttackTest = 0; } } }
もともとは下のEnemyMoveスクリプトで3秒に一回普通の球を発射していました。
それを変更したく、ここにある普通の球の発射の関数EnemyBallを上記スクリプトに移動しました。(移動した理由は、上記スクリプトからこのスクリプトの関数の実行に手間取ったからです)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMove : MonoBehaviour { public GameObject ball; float speed; public float span = 3f; public float currentTime = 0f;// // Start is called before the first frame update void Start() { speed = 4.0f; } // Update is called once per frame void Update() { Vector3 tmp = GameObject.Find("HeavyHullD").transform.position; currentTime += Time.deltaTime; //if(currentTime > span){ // EnemyBall(); // currentTime = 0f; //} if (Input.GetMouseButtonDown(0)) { } void EnemyBall(){ // 弾(ゲームオブジェクト)の生成 GameObject clone = Instantiate(ball, transform.position, Quaternion.identity); // クリックした座標の取得(スクリーン座標からワールド座標に変換) Vector3 mouseWorldPos = tmp; // 向きの生成(Z成分の除去と正規化) Vector3 shotForward = Vector3.Scale((mouseWorldPos - transform.position), new Vector3(1, 1, 0)).normalized; // 弾に速度を与える clone.GetComponent<Rigidbody2D>().velocity = shotForward * speed; } } void OnCollisionEnter2D(Collision2D collision){ if(collision.gameObject.tag == "Ball"){ Debug.Log("hitEnemy"); Destroy(this.gameObject); } } }

回答1件
あなたの回答
tips
プレビュー