当一个脚本到达目标玩家时,我如何让我的敌人禁用它?我想在我的代码中引用这个在我的GameManager下称为强制转换的脚本,并禁用它,但是我不确定当到达目标位置时如何编写函数。
public float lookRadius = 40f;
Transform target;
UnityEngine.AI.NavMeshAgent agent;
Rigidbody theRigidBody;
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update()
{
float distance = Vector3.Distance (target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination (target.position);
if (distance <= agent.stoppingDistance)
{
FaceTarget ();
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
}
// Use this for initialization
public void OnObjectSpawn ()
{
//myRender = GetComponent<Renderer> ();
theRigidBody = GetComponent<Rigidbody>();
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position, lookRadius);
}发布于 2018-04-12 03:07:20
您需要获取包含脚本的游戏对象,获取该游戏对象上的脚本,然后禁用该脚本。
if (distance < 1f) // or some distance
{
Gameobject mygo = GameObject.Find("GameManager"); // or some other method to get gameobject.
mygo.GetComponent<casting>().enabled = false;
}https://stackoverflow.com/questions/49782686
复制相似问题