Search Unity

Question Stop counting score in OnTriggerStay

Discussion in 'VR' started by VCUMCL, Feb 28, 2023.

  1. VCUMCL

    VCUMCL

    Joined:
    Jun 5, 2017
    Posts:
    24
    I currently am designing a VR game where the user will be using a 3D Systems haptics arm to control a virtual pointer. When the pointer enters an object's trigger, it should deduct a certain amount points from their total.

    However, the problem I have is when the pointer stays inside the object. If I leave the pointer in the object and move it around in it, points will continuously reduce. Is this intentional? If not, I would hope that
    void OnTriggerStay()
    would help me fix that. I would just need to figure out how to prevent the game from deducting points while the pointer is in the object.
     
  2. nilagard

    nilagard

    Joined:
    Jan 13, 2017
    Posts:
    77
    It would certainly help seeing the code you have when trying to implement this. You might have forgotten something when creating the logic for this.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    LeviathanLevi and nilagard like this.
  4. VCUMCL

    VCUMCL

    Joined:
    Jun 5, 2017
    Posts:
    24
    This is the code in question:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.     if (scene.name == "AlmostThere")
    3.         {
    4.             if (other.gameObject.name == "default (1)")
    5.             {
    6.                 inTrigger = true;
    7.                 print("Hit bladder!");
    8.                 Text bladderText = GameObject.Find("Haptic_And_Camera/[CameraRig]/Canvas/Panel/BladderText").GetComponent<Text>();
    9.                 print(bladderText);
    10.                 bladderText.text = "Bladder: Collision!";
    11.                 bladderText.color = Color.red;
    12.                 buzz.Play();
    13.                 this.GetComponent<Renderer>().material.color = Color.red;
    14.  
    15.                 int.TryParse(overallText.text, out overallNumber);
    16.                 overallNumber -= 10;
    17.                 overallText.text = overallNumber.ToString();
    18.  
    19.                 if (overallNumber < 0)
    20.                 {
    21.                     overallNumber = 0;
    22.                     overallText.text = "0";
    23.                 }
    24.  
    25.                 if (overallNumber == 0)
    26.                 {
    27.                     failed.SetActive(true);
    28.                 }
    29.             }
    30.  
    31.         }
    32.  
    33.     }
     
  5. LeviathanLevi

    LeviathanLevi

    Joined:
    Mar 29, 2022
    Posts:
    4
    As mgear mentioned OnTriggerEnter() should work

    Also check the collider bounds on both the hitting object and the object being hit, are they colliding multiple times because they're too small or something?

    Instead of checking the game objects name you could filter by tag instead
     
  6. VCUMCL

    VCUMCL

    Joined:
    Jun 5, 2017
    Posts:
    24
    The object in question is this probe with a hand attached to it. Ideally, only the end of this probe would be the part colliding with other objects.

    upload_2023-3-9_8-25-31.png

    The probe itself is the one with a collider, I removed the collider from the hand. The collider itself is quite unconventional.