Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Need help making OnTriggerEnter change float value by only 1

Discussion in 'Scripting' started by jojomonster679, Apr 4, 2019.

  1. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    I am making a racing demo and every time I pass through the starting line, I want the value of the laps completed to increase by only 1 so that when I complete three laps, my timer starts. As you may assume, my timer starts and stops immediately because of the time my car takes to make it through the starting line trigger. Is there a way for me to make it so that the OnTriggerEnter function waits a few seconds before checking for a trigger entrance again? Thanks!

    Here's my code for the trigger:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class startStop : MonoBehaviour
    6. {
    7.  
    8.     private float lap = 0f;
    9.  
    10.     private void OnTriggerEnter(Collider other)
    11.     {
    12.         if(lap <= 3)
    13.         {
    14.             GameObject.FindGameObjectWithTag("Car").SendMessage("StartTimer");
    15.             lap++;
    16.             Debug.Log(lap);
    17.         }
    18.         if(lap > 3)
    19.         {
    20.             GameObject.FindGameObjectWithTag("Car").SendMessage("Finish");
    21.         }
    22.     }
    23.  
    24. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't use a float unless you need it to be a float. Unless you're tracking partial laps there's no reason to use floats here.

    Instead of waiting to start adding laps, just use OnTriggerExit to set a bool saying it is ok to start incrementing laps in future OnTriggerEnter calls.
     
  3. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    I tried increasing the laps in ontriggerexit, but it still counts by more than one. Could you give an example of how to do it?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If I understand your issue, you have cars starting behind the starting line and when they first cross it you don't want it to increment the first time, right?

    So I wasn't talking about incrementing in OnTriggerExit. I was talking about not incrementing in OnTriggerEnter until OnTriggerExit is called.

    Code (csharp):
    1. int laps = 0;
    2. bool trackLaps = false;
    3.  
    4. void OnTriggerEnter(Collider collider)
    5. {
    6.     if (trackLaps)
    7.     {
    8.         laps++;
    9.     }
    10. }
    11.  
    12. void OnTriggerExit(Collider collider)
    13. {
    14.     trackLaps = true;
    15. }
    Alternatively you could just start with laps set to -1, knowing it goes to 0 as soon as you pass the start line. If you are displaying this in text to the player you just print 0 if it is ever less than 0.

    Code (csharp):
    1. int laps = -1;
    2.  
    3. void OnTriggerEnter(Collider collider)
    4. {
    5.     laps++;
    6. }
    None of the above was meant to be a rewrite of your code, I just wrote it as an example.
     
  5. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    My issue is not with actually counting the laps. The issue is that whenever I enter or exit the trigger, it increments my laps by more than one.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Do you have more than 1 car on the track, or more than one collider on the car? Check what the name of the object is that is calling the trigger. As written, any collider which enters the trigger will call OnTriggerEnter, and you never check what it is before incrementing.
     
    jojomonster679 likes this.
  7. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    THANK YOU! I realized that there were many elements that were children of my car that had colliders so I decided to make it so that the ontriggerenter event only triggers if one of the specific wheel colliders passes through the trigger.
     
    Joe-Censored likes this.