开门时超过20个硬币
本文关键字:20个 硬币 | 更新日期: 2023-09-27 18:13:27
我尝试着让玩家在收集到20个硬币时打开unity的一扇门。当你碰它的时候它就会打开。但出于某种原因。当我用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;应该是>。但现在的问题是,因为这是一个触发器,你可以直接滚过去。我怎样才能让它既坚固又能触发?
else if (other.gameObject.CompareTag("door") && count <= 20)
你的意思是如果计数小于OR 20,那么开门。
改为:
else if (other.gameObject.CompareTag("door") && count >= 20)
则表示至少有20个或更多
本教程可能会让您了解您可以使用的操作符:)