简体   繁体   中英

How to increment/decrement only by one when Ray hits

So, the game is to find a difference. When ray hits a collider of hidden object, it is supposed to add a score only of one and reduce a private integer differences value by one. But when the ray hits an object collider it is constantly incrementing/decrementing while the ray is hitting the collider. How can i change that? It is in the following method:


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

        
    }

When you first hit the object, you could remove or disable it's collider component. This should prevent it from being hit by more Raycasts.

On first hit, something like:

result.collider.enabled = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM