Unity GameObject.FindGameObjectsWithTag(" Player")。transform.position;

时间:2017-07-13 18:22:19

标签: c# unity3d

你好,我有问题我需要使用标签播放器获取更多1个对象的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);
            }
        }

    }    
}

1 个答案:

答案 0 :(得分:3)

FindGameObjectsWithTag returns an array和数组没有.transform属性。

你需要使用一个循环并迭代结果(可能找到最近的?)。

此外,在您的脚本中,Player属于Transform类型,但您尝试将.transform.position(类型为Vector3)的值分配给它。