【问题标题】:Open door when more than 20 coins超过20个硬币时开门
【发布时间】:2016-10-18 13:00:30
【问题描述】:

我正在努力做到这一点,以便在您收集到 20 个硬币时打开一扇统一的门。然后当你触摸它时它会打开。但出于某种原因。当我用 0、1、2 等硬币触摸它时,它仍然会打开。我该如何防止这种情况?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class Player_Controller : MonoBehaviour {
public float speed;
public Text countText;
private Rigidbody rb;
private int count;
public float volume;
public Text eye;



AudioSource audio;
void Start()
{

    audio = GetComponent<AudioSource>();
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText();
    eye.text = "";
}


void FixedUpdate()
{

    float moveHorizonal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizonal, 0.0f, moveVertical);

    rb.AddForce(movement * speed);

}
void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("pickup"))
    {
        audio.Play(); //Play it

        other.gameObject.SetActive(false);
        count = count + 1;
        SetCountText();
    }
    else if (other.gameObject.CompareTag("pickup2"))
    {
        audio.Play(); //Play it

        other.gameObject.SetActive(false);
        count = count + 10;
        SetCountText();
    }
    else if (other.gameObject.CompareTag("eye"))
    {
        audio.Play(); //Play it

        other.gameObject.SetActive(false);
        count = count + 9999999;
        SetCountText();
    }
    else if (other.gameObject.CompareTag("door") && count <= 20)
    {
        other.gameObject.SetActive(false);
        count = 0;
    }


}

void SetCountText()
{
    countText.text = "Count: " + count.ToString();
    if (count >= 9001)
    {
        eye.text = "You hit the bull's eye! ALL THE POINTS!";
    }
}
}

更新:我在这里使用了错误的字符。 。但是现在的问题是,由于它是触发器,因此您可以直接通过。我怎样才能让它稳固,但仍然是一个触发器?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    else if (other.gameObject.CompareTag("door") && count <= 20)
    

    你是说如果计数小于或 20 则打开门。

    改成:

    else if (other.gameObject.CompareTag("door") && count >= 20)
    

    这说明至少有 20 个或更多

    这个tutorial 可能会对您可以使用的运算符有所了解:)

    【讨论】:

    • 是的,我认为我做错了这样的事情。但问题是现在。无论我是否有 20 分,我都可以滚过门。我知道这是因为它是一个触发器。那么有没有办法让触发器仍然有碰撞?
    • @MichaDeHaan 恐怕我不适合回答这个问题,我建议在这里搜索/将其作为一个单独的问题提出;我刚刚看到一个 C# 问题:P
    • 实际上我已经想通了:我所要做的就是在坚固的门上添加一个孩子。因为当您删除父级时,所有子级也会被删除。无论如何,谢谢!
    猜你喜欢
    • 2020-12-16
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多