当我尝试在调试时启用交互脚本时,只要我设置了一个断点,它就可以完美运行。但是当我在不调试的情况下运行游戏时,大约有 50% 的时间无法启用脚本。Interact 是 NPC 和 MonsterAttack 脚本的基类。我只有 NPC 和 MonsterAttack 脚本附加到物理游戏对象。
void Update ()
{
if (Input.GetMouseButton(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
if (interactedObject != null && interactedObject.GetComponent<Interact>() != null)
{
interactedObject.GetComponent<Interact>().enabled = false;
}
Interact.rotate = false;
rayHit = GetInteraction();
}
}
//Get interaction with clicked object
private RaycastHit GetInteraction()
{
//Get the mouse clicked position
Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit interactionInfo;
if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
{
Debug.DrawRay(interactionRay.origin, interactionInfo.point, Color.red);
interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag != "NPC" && interactedObject.tag != "Monster")
{
//Move somewhere on the terrain
playerAgent.stoppingDistance = 0f;
playerAgent.SetDestination(interactionInfo.point);
}
else
{
//IT FAILS HERE
interactedObject.GetComponent<Interact>().enabled = true; //<--------
//Interact with an Object, NPC, Item, Monster
interactedObject.GetComponent<Interact>().MovetoInteraction(playerAgent);
}
}
return interactionInfo;
}
千巷猫影
相关分类