【发布时间】:2017-02-07 23:54:09
【问题描述】:
我需要一些关于我正在为使用 Unity 2D 制作的游戏实现的功能的帮助。 玩家必须拿钥匙才能打开一扇门(可能会显示动画),当玩家走到那扇门前时,如果他有钥匙,他会自动进入下一个关卡。 我需要帮助,因为门无法进入下一层。
这是关键代码/脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class KeyScript : MonoBehaviour {
//public AudioSource coinSoundEffect;
public AudioClip key1;
void Awake () {
//source = GetComponent<AudioSource>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D other) {
Debug.Log("Chiave Presa");
if(other.gameObject.tag =="Player")
GameObject.Find("KeyDoor").SendMessage("HitKey");
SoundManager2D.playOneShotSound(key1);
}
}
这是 DOOR 代码/脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class DoorScript : MonoBehaviour {
public bool key = false;
public string nextLevelName;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void HitKey (){
Debug.Log("La porta è sbloccata");
key = true;
Destroy (GameObject.Find ("Key"));
}
void OnCollisionEnter2D (Collision2D other){
if(other.gameObject.tag == "Player"){
if (key == true){
Application.LoadLevel(nextLevelName);
//Destroy(gameObject);
//GameObject.Find("Key").SendMessage("DestroyKey"); //If you also want to destroy the key
//GoToNextLevel ();
}
}
}
void OnTriggerEnter2D (Collision2D other)
{
Application.LoadLevel(nextLevelName);
}
public virtual void GoToNextLevel()
{
//loadingImage.SetActive(true);
Application.LoadLevel(nextLevelName);
}
}
代码有效,但是当玩家走到门前时,他并没有进入下一关。 任何帮助或提示表示赞赏。 干杯
【问题讨论】:
-
你试过
GameObject.Find("KeyDoor").GetComponent<DoorScript>().SendMessage("HitKey");吗?或者更简单的GameObject.Find("KeyDoor").GetComponent<DoorScript>().key = true; -
@m.rogalski 我把这个 GameObject.Find("KeyDoor").GetComponent
放在关键脚本上,但还是不行。 -
你确定所有的
GameObjects 都有他们的Colliders 连接并且工作吗? -
@m.rogalski 现在工作了,但我有另一个问题,进入门脚本我想以另一种方式加载下一个级别,因为我写的方式并不容易。这意味着我需要为每个级别的门制作脚本。