教科書を見ながらコードを記述しているのですが何故かエラーが出てしまします。原因を調べたところ使用できないコードでバージョンが古いそうです(教科書のサンプルが古い)、ですので敵が数体いる場所で自身から一番近い敵をロックオンするにはどうすればいいでしょうか??
御教授願いたいです。
public class LockOn : MonoBehaviour {
GameObject target = null; // Use this for initialization void Start () { } // Update is called once per frame void Update() { if (Input.GetButtonDown("Lock")) { //ロックを解除する if (target != null) target = null; else //一番近いターゲットを取得する taregt = FindClosestEnemy(); ←ここでエラー //ターゲットを取得 //target = GameObject.FindWithTag("Enemy"); } if(target != null) { //ターゲットの方を向く //transform.LookAt(target.transform); //スムーズにターゲットの方を向く Quaternion targetRotation = Quaternion.LookRotation (target.transform.position - transform.position); transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10); transform.rotation = new Quaternion (0, transform.rotation.y,0, transform.rotation.w); //カメラをターゲットに向ける Transform cameraParent = Camera.main.transform.parent; Quaternion targetRotation2 = Quaternion.LookRotation (target.transform.position - cameraParent.position); cameraParent.localRotation = Quaternion.Slerp (cameraParent.localRotation, targetRotation2, Time.deltaTime * 10); cameraParent.localRotation = new Quaternion(cameraParent.localRotation.x, 0, 0, cameraParent.localRotation.w); } //一番近い敵を探して取得 GameObject FindClosestEnemy() { ←ここでエラー //GameObject.FindWithTag("Enemy"); //{ GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("Enemy"); GameObject closest = null; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach(GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if(curDistance < distance) { closest = go; distance = curDistance; } } } } }
回答1件
あなたの回答
tips
プレビュー