打开门时超过20个硬币

时间:2016-10-18 13:00:31

标签: c# unity3d

我试图让它成为一个团结的门,当你收集了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!";
    }
}
}

更新:我在这里使用了错误的字符。 &lt;应该是&gt;。但现在的问题是,由于它是一个触发器,你可以直接滚动。我怎样才能让它变得坚实,但仍然是一个触发器?

1 个答案:

答案 0 :(得分:0)

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

你说如果计数小于OR 20,那就打开门。

将其更改为:

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

然后说至少20或更多

tutorial可能会对您可以使用的运营商有所了解:)