C# 我如何才能禁用游戏,使其对每个对象计数超过一个?
我正在团结一致地使用C#,并试图练习 我希望游戏只在球碰撞每个立方体一次时计数,但当它发现它们之间发生碰撞时,似乎同一个立方体的球计数相同,那么我如何才能使预制中的对象中的对象被禁用以进行更改,但仍然可见C# 我如何才能禁用游戏,使其对每个对象计数超过一个?,c#,unity3d,unity5,C#,Unity3d,Unity5,我正在团结一致地使用C#,并试图练习 我希望游戏只在球碰撞每个立方体一次时计数,但当它发现它们之间发生碰撞时,似乎同一个立方体的球计数相同,那么我如何才能使预制中的对象中的对象被禁用以进行更改,但仍然可见 using UnityEngine; using System.Collections; using UnityEngine.UI; public class PlayerController : MonoBehaviour { public float speed; pu
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 ();
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共类玩家控制器:单行为{
公众浮标速度;
公共文本;
私人刚体;
私人整数计数;
无效开始()
{
rb=GetComponent();
计数=0;
SetCountText();
}
无效固定更新()
{
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
Vector3移动=新Vector3(水平移动,0.0f,垂直移动);
rb.AddForce(移动*速度);
}
无效对撞机(对撞机其他)
{
if(other.gameObject.CompareTag(“拾取”))
{
//它如何知道预置中哪个标记为“拾取”的对象被选中?如何使其在碰撞后第二次被球禁用?
计数=计数+1;
SetCountText();
}
}
void SetCountText()
{
countText.text=“Count:+Count.ToString();
}
}
在我看来,您有两种选择。其中之一是:
立方体对象
- 在多维数据集上设置一个
变量<代码>布尔值hasBeenHit或类似值。这将启动attboolean
李>false
- 为多维数据集提供一个
,在其中设置OnTriggerEnter()
hasBeenHit=true
- 为hasBeenHit制作一个公共getter
- 在
中,检查ontriggenter(碰撞器其他)
是否为立方体other
- 如果它是一个立方体,请检查它的
是否为hasBeenHit
或true
false
- 如果是
,则递增false
count
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...
}
}
您想要
OnTriggerCenter()
还是OnCollisionCenter()
?在多维数据集中创建一个脚本,并添加一个onCollisionCenter()
,当检测到与球发生碰撞时,它会在其中更改标记或其他内容