我怎样才能禁止游戏为每个对象计入一个以上?
How can I disable the game to count for more than one for each object?
我正在将 C# 与 Unity 结合使用并尝试练习 this example
我希望游戏只在球与每个立方体碰撞一次时计算一次,但似乎只要发现它们之间发生碰撞,同一个立方体的球就会计数,所以我怎样才能使预制件中的对象中的对象禁用为已更改但仍然可见?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
// How it can know which one of the object in the prefab that tagged to "Pick Up" is selected ? and how can make it disabled to be effected by the ball in the second time after the collide ?
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
}
}
据我所知,您有多种选择。其中之一是:
CUBE对象
- 在你的多维数据集上设置一个
boolean
变量。 boolean hasBeenHit
或类似的。这从 att false
开始。
- 给立方体一个
OnTriggerEnter()
,它设置了hasBeenHit = true
。
- public getter
hasBeenHit
.
球对象
- 在
OnTriggerEnter(Collider other)
中检查 other
是否是立方体。
- 如果是立方体,检查它的
hasBeenHit
是true
还是false
。
- 如果是
false
,递增count
如果你摧毁了"other"对撞机,那么它就不能再碰撞了。或者,如果您愿意,也可以禁用它,但这意味着同样的事情。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
// How it can know which one of the object in the prefab that tagged to "Pick Up" is selected ? and how can make it disabled to be effected by the ball in the second time after the collide ?
count = count + 1;
SetCountText ();
Destroy(other); // Add this...
}
}
我正在将 C# 与 Unity 结合使用并尝试练习 this example 我希望游戏只在球与每个立方体碰撞一次时计算一次,但似乎只要发现它们之间发生碰撞,同一个立方体的球就会计数,所以我怎样才能使预制件中的对象中的对象禁用为已更改但仍然可见?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
// How it can know which one of the object in the prefab that tagged to "Pick Up" is selected ? and how can make it disabled to be effected by the ball in the second time after the collide ?
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
}
}
据我所知,您有多种选择。其中之一是:
CUBE对象
- 在你的多维数据集上设置一个
boolean
变量。boolean hasBeenHit
或类似的。这从 attfalse
开始。 - 给立方体一个
OnTriggerEnter()
,它设置了hasBeenHit = true
。 - public getter
hasBeenHit
.
球对象
- 在
OnTriggerEnter(Collider other)
中检查other
是否是立方体。 - 如果是立方体,检查它的
hasBeenHit
是true
还是false
。 - 如果是
false
,递增count
如果你摧毁了"other"对撞机,那么它就不能再碰撞了。或者,如果您愿意,也可以禁用它,但这意味着同样的事情。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
// How it can know which one of the object in the prefab that tagged to "Pick Up" is selected ? and how can make it disabled to be effected by the ball in the second time after the collide ?
count = count + 1;
SetCountText ();
Destroy(other); // Add this...
}
}