Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何才能禁用游戏,使其对每个对象计数超过一个?_C#_Unity3d_Unity5 - Fatal编程技术网

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

我正在团结一致地使用C#,并试图练习 我希望游戏只在球碰撞每个立方体一次时计数,但当它发现它们之间发生碰撞时,似乎同一个立方体的球计数相同,那么我如何才能使预制中的对象中的对象被禁用以进行更改,但仍然可见

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();
}
}

在我看来,您有两种选择。其中之一是:

立方体对象

  • 在多维数据集上设置一个
    boolean
    变量<代码>布尔值hasBeenHit或类似值。这将启动att
    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()
,当检测到与球发生碰撞时,它会在其中更改标记或其他内容