如何解决在预制件的多个副本上触发探路者NavMesh功能
我正在Unity3D中进行实验,我想制作一个COin召唤机制。这个想法是,如果玩家触摸某个特定对象,那么玩家周围的每个硬币都会按照我设置的NavMesh到达玩家手中。我的问题是代码只能在一个硬币上工作。即使所有硬币都是同一预制件的副本,一旦功能触发,只有一个硬币开始移动。您能找出错误所在吗?我知道这是关于单例和静态类的。我还不完全了解。 我的代码- 这个脚本在硬币里
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class MagnetCoinMovement : MonoBehavIoUr
{
public Transform player;
public NavMeshAgent agent;
bool CoinMovement = false;
void Update()
{
if (CoinMovement == true)
{
agent.SetDestination(player.transform.position);
}
}
public void Coinmobility()
{
CoinMovement = true;
}
}
这是触发NavMesh函数的脚本-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinTrigger : MonoBehavIoUr
{
public MagnetCoinMovement trigger;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
trigger.Coinmobility();
}
}
}
解决方法
看看是否可行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinTrigger : MonoBehaviour
{
private bool activated = false;
private void OnTriggerEnter(Collider other)
{
if (activated == false && other.gameObject.CompareTag("Player"))
{
activated = true;
MagnetCoinMovement[] coins = FindObjectsOfType<MagnetCoinMovement>();
for (int i = 0; i < coins.Length; i++)
{
coins[i].CoinMobility();
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。