我正在遵循this统一3D课程。我遵循称为“敌人第1部分:静态观察者”的部分的每个步骤,在重新检查代码并进行了一天的研究之后,我仍然没有发现问题。本部分教程的范围是使当“石像鬼”看到玩家时,如果在玩家面前经过玩家,则应重新启动游戏。
这是两个应该起作用的脚本,但不是。
观察者(石像鬼):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Observer : MonoBehaviour
{
public Transform player;
public GameEnding gameEnding;
bool m_IsPlayerInRange;
void OnTriggerEvent(Collider other)
{
if (other.transform == player)
{
m_IsPlayerInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.transform == player)
{
m_IsPlayerInRange = false;
}
}
void Update()
{
if (m_IsPlayerInRange)
{
Vector3 direction = player.position - transform.position + Vector3.up;
Ray ray = new Ray(transform.position, direction);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit))
{
if (raycastHit.collider.transform == player)
{
gameEnding.CaughtPlayer();
}
}
}
}
}
这是GameEnding脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameEnding : MonoBehaviour
{
public float fadeDuration = 1f;
public float displayImageDuration = 1f;
public GameObject player;
public CanvasGroup exitBackgroundImageCanvasGroup;
public CanvasGroup caughtBackgroundImageCanvasGroup;
bool m_IsPlayerAtExit;
bool m_IsPlayerCaught;
float m_Timer;
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
m_IsPlayerAtExit = true;
}
}
public void CaughtPlayer()
{
m_IsPlayerCaught = true;
}
void Update()
{
if (m_IsPlayerAtExit)
{
EndLevel(exitBackgroundImageCanvasGroup, false);
}
else if (m_IsPlayerCaught)
{
EndLevel(caughtBackgroundImageCanvasGroup, true);
}
}
void EndLevel(CanvasGroup imageCanvasGroup, bool doRestart)
{
m_Timer += Time.deltaTime;
imageCanvasGroup.alpha = m_Timer / fadeDuration;
if (m_Timer > fadeDuration + displayImageDuration)
{
if (doRestart)
{
SceneManager.LoadScene(0);
}
else
{
Application.Quit();
}
}
}
}
回到统一编辑器,我设置了变量(玩家,游戏结束,exitimagebackground和捕获的imagebackground。
有人知道问题出在哪里并且可以帮助我吗?
谢谢!
和石像鬼的这些
其中有哪些是孩子:
具有以下其他组件:
答案 0 :(得分:0)
在您的Observer
班上,
void OnTriggerEvent(Collider other)
{
if (other.transform == player)
{
m_IsPlayerInRange = true;
}
}
函数名is OnTriggerEvent(Collider)
,应改为OnTrigger**Enter**(Collider)
。
否则,它将按预期工作。