今、課題でunityを使用し、横スクロールアクションのゲームを作っているのですが、ホーミング弾の挙動がバラバラで近くの敵にホーミング弾がとんでいかない状況です。(サイトやサンプルコードを参照させていただきながら作成しています。)FindGameObjectsWithTagを使う必要があると回答してくださったので検索し、色々調べてみましたが、今一書き方が分かりませんでした。教えて下さると幸いです。
参照させていただいたサンプルコード
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class enemyhoming1 : MonoBehaviour
{
Vector2 A, C, AB, AC; // ベクトル
Transform target; // 追いかける対象 public float speed; // 移動スピード public float maxRot; // 曲がる最大角度 // Use this for initialization void Start() { target = GameObject.Find("Enemy").transform; transform.eulerAngles += new Vector3(0, 0, Sita()); // ターゲットの方向を向く GetComponent<Rigidbody2D>().velocity = transform.up.normalized * speed; } // Update is called once per frame void Update() { Move(Sita()); // 移動処理 } //----------------------------------------------------------------------------------------------- // なす角θを求める //----------------------------------------------------------------------------------------------- float Sita() { A = transform.position; // 自身の座標 C = target.position; // ターゲットの座標 AB = transform.up; // 自身の上方向ベクトル AC = C - A; // ターゲットの方向ベクトル float dot = Vector3.Dot(AB, AC); // 内積 float rot = Acosf(dot / (Length(AB) * Length(AC))); // アークコサインからθを求める // 外積から回転方向を求める if (AB.x * AC.y - AB.y * AC.x < 0) { rot = -rot; } return rot * 180f / Mathf.PI; // ラジアンからデグリーに変換して角度を返す } //----------------------------------------------------------------------------------------------- // 移動処理 //----------------------------------------------------------------------------------------------- void Move(float rot) { // 求めた角度が曲がる最大角度より大きかった場合に戻す処理 if (rot > maxRot) { rot = maxRot; } else if (rot < -maxRot) { rot = -maxRot; } transform.eulerAngles += new Vector3(0, 0, rot); // 回転 GetComponent<Rigidbody2D>().velocity = AB * speed; // 上に移動 } /// <summary> /// ベクトルの長さを求める /// </summary> /// <param name="vec">2点間のベクトル</param> /// <returns></returns> float Length(Vector2 vec) { return Mathf.Sqrt(vec.x * vec.x + vec.y * vec.y); } /// <summary> /// Acosの引数の値が+-1を越えたとき1に戻すAcos関数 /// </summary> /// <param name="a">内積 / (ベクトルの長さ * ベクトルの長さ)</param> /// <returns></returns> float Acosf(float a) { if (a < -1) a = -1; if (a > 1) a = 1; return (float)Mathf.Acos(a); }
}

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。