你好,我有我的问题,我需要得到的转换。位置超过1对象与标签播放器,使脚本追逐多个播放器
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);
}
}
}
}
2条答案
按热度按时间k5ifujac1#
FindGameObjectsWithTag
returns an array和数组没有.transform
属性。您需要使用一个循环并迭代结果(可能是为了定位 nearest?)。
此外,在脚本中,
Player
的类型为Transform
,而您却试图将.transform.position
(类型为Vector3)的值赋给它。fhity93d2#
添加游戏对象。查找带有标签的游戏对象(“玩家”)[0]。转换。位置;