using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
[SerializeField] AudioClip成功。
[SerializeField] AudioClip crash;
[SerializeField] ParticleSystem successParicle;
[SerializeField] ParticleSystem crashParicle;
AudioSource音頻源。
float delayTime = 2f;
bool isTransitioning = false;
void Start()
{
audioSource = GetComponent<AudioSource>()。
}
void OnCollisionEnter(Collision other)
{
if(isTransitioning)
{
return;
}
switch (other.gameObject.tag)
{
case "Friendly"/span>:
Debug.Log("It's OK friend")。
break;
case "完成":
SuccessSequence()。
break;
default:
CrashSequence()。
break;
}
void SuccessSequence()
{
isTransitioning = true;
audioSource.Stop();
successParicle.Play();
audioSource.PlayOneShot(success);
GetComponent<Shutle>().enabled = false;
Invoke(nameof(NextScene), delayTime)。
//NextScene();
}
void CrashSequence()
{
isTransitioning = true;
audioSource.Stop()。
crashParicle.Play()。
audioSource.PlayOneShot(crash);
GetComponent<Shutle>().enabled = false;
Invoke(nameof(ReloadScene), delayTime)。
/ReloadScene();
}
void ReloadScene() /dit gebruiken wij om respown met gebruik van scene reload funcite te gebruiken)
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex)。
}
void NextScene()?
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex 1;
if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex)。
}
}
}
我正在構建一個基本的游戲,其中一艘船從一個地方飛到另一個地方。當它崩潰或成功時,它應該在重新出現或移動到下一關之前做幾個功能。所以我需要呼叫這些函式。 "Trying to Invoke method: CollisionHandler.NextScene不能被呼叫。"
uj5u.com熱心網友回復:
你的方法都嵌套在OnCollisionEnter下,作為locel函式,有什么好的理由嗎? ;)
Unity的訊息傳遞系統并沒有找到區域函式--因為它們實際上只存在于這一個方法中--而是只對類級方法起作用。
這應該是
priate void OnCollisionEnter(Collision other)
{
if(isTransitioning)
{
return;
}
switch (other.gameObject.tag)
{
case "Friendly"/span>:
Debug.Log("It's OK friend")。
break;
case "完成":
SuccessSequence()。
break;
default:
CrashSequence()。
break;
}
}
priate void SuccessSequence()
{
isTransitioning = true;
audioSource.Stop();
successParicle.Play();
audioSource.PlayOneShot(success);
GetComponent<Shutle>().enabled = false;
Invoke(nameof(NextScene), delayTime)。
//NextScene();
}
priate void CrashSequence()
{
isTransitioning = true;
audioSource.Stop()。
crashParicle.Play()。
audioSource.PlayOneShot(crash);
GetComponent<Shutle>().enabled = false;
Invoke(nameof(ReloadScene), delayTime)。
/ReloadScene();
}
priate void ReloadScene() /dit gebruiken wij om respown met gebruik van scene reload funcite te gebruiken
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex)。
}
priate void NextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex 1;
if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex)。
}
作為Invoke的替代方案,你可以把它變成所有的例程,在我看來,這比 "神奇 "的Invoke呼叫更好控制、理解和維護;)
適當的IEnumerator OnCollisionEnter(Collision other)
{
if(isTransitioning)
{
yield break;
}
switch (other.gameObject.tag)
{
case "Friendly"/span>:
Debug.Log("It's OK friend")。
break;
case "完成":
yield return SuccessSequence()。
break。
default:
yield return CrashSequence()。
break。
}
}
適當的IEnumerator SuccessSequence()。
{
isTransitioning = true;
audioSource.Stop();
successParicle.Play();
audioSource.PlayOneShot(success);
GetComponent<Shutle>().enabled = false;
yield return new WaitForSeconds(delayTime)。
下一個場景()。
}
適當的IEnumerator CrashSequence(); NextScene()
{
isTransitioning = true;
audioSource.Stop()。
crashParicle.Play()。
audioSource.PlayOneShot(crash);
GetComponent<Shutle>().enabled = false;
yield return new WaitForSeconds(delayTime)。
ReloadScene()。
}
priate void ReloadScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex)。
}
priate void NextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex 1;
if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312670.html
標籤:
