2017-07-13 148 views
-1

你好,我有我的問題,我需要得到更多然後1個對象與標籤玩家的transform.position所以劇本追逐多個玩家Unity GameObject.FindGameObjectsWithTag(「Player」)。transform.position;

using System.Collections.Generic; 
using UnityEngine; 

public class enemyChase: MonoBehaviour 
{ 
    //private GameObject[] Player; 
    Transform Player; 

    // Use this for initialization 
    void Start() 
    { 
     Player = GameObject.FindGameObjectsWithTag ("Player").transform.position; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Vector3.Distance (Player.position, this.transform.transform.position) < 10) { 
      Vector3 direction = Player.position - this.transform.position; 

      this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation(direction), 0.1f); 
      if (direction.magnitude > 1) { 

       this.transform.Translate (0,0,0.05f); 
      } 
     } 

    }  
} 
+0

Your'Player = GameObject.FindGameObjectsWithTag(「Player」)。transform.position;'''GameObjects'的'Array'數組。所以你應該1)用'Player = GameObject.FindGameObjectWithTag(「Player」)。transform.position;'或2)稍後在'Player'數組上循環。 – KamikyIT

回答

3

FindGameObjectsWithTagreturns an array和數組沒有.transform財產。

您需要使用循環並遍歷結果(可能需要找到最接近的)。

此外,在你的腳本,PlayerTransform類型的,但你嘗試分配的.transform.position值(類型的Vector3),以它。

+0

所以它如何看起來我是一個大先生 – Ghigh

+1

@高我推薦[看一些文檔](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/using-foreach-with - 陣列)或[遵循一些教程](https://www.youtube.com/watch?v=wOVu5bQxnbA) – Draco18s