如何在雷击中时increment/decrement只差一个

How to increment/decrement only by one when Ray hits

所以,游戏就是找不同。当射线撞击隐藏物体的碰撞器时,它应该只增加一个分数,并将一个私有整数差值减一。但是当光线撞击物体碰撞器时,它会不断地 incrementing/decrementing 而光线正在撞击碰撞器。我怎样才能改变它?它在以下方法中:


void Update()
    {

        camRay = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
        Debug.DrawRay(camRay.origin, camRay.direction, Color.red, 1);

        if (Input.touchCount > 0)
        {
            if (Physics.Raycast(camRay, out result))
            {
                /*{
                    obj = result.collider.gameObject;
                    score = score + 1;
                    differences= differences - 1;
                    score1.text = " " + score;
                    predmet.Play();                
                }*/

                if (result.collider.CompareTag("Slike") && !hitOnce)
                {
                    score +=1;
                    differences-= 1;
                    score1.text = " " + score;
                    predmet.Play();
                }
                else
                {
                    hitOnce = false;
                }

                if (differences == 0)
                {
                    WinPanel.SetActive(true);
                }
            }
        
        }       

        
    }

当你第一次击中物体时,你可以移除或禁用它的碰撞器组件。这应该可以防止它被更多的 Raycasts 击中。

第一次点击时,类似于:

result.collider.enabled = false;