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. Dismiss Notice

How to know that the car is going back (wrong way)

Discussion in 'Editor & General Support' started by mprimo, Oct 8, 2018.

  1. mprimo

    mprimo

    Joined:
    Aug 1, 2018
    Posts:
    42
    im making a racing game and i want to check if the car going on the wrong way (going back) or not?
    please can u help me?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How are you checking if the car is going the correct way? Because I'd just check for the opposite of however you are doing that. For example, if you have a series of check points, you can see they are going the correct way if the distance to the next check point is decreasing while the distance to the previous checkpoint is increasing. They would be going the wrong way if the opposite were true.
     
  3. mprimo

    mprimo

    Joined:
    Aug 1, 2018
    Posts:
    42
    I didn't check if the car going the correct way.

    Yes

    Very good idea, i will try it and tell you the result.
    Thanks in advance.
     
    Joe-Censored likes this.
  4. mprimo

    mprimo

    Joined:
    Aug 1, 2018
    Posts:
    42
    Hey,
    I made it by checking (first checkpoint z - player position z) is increasing but the its bad because sometimes car going in x axis not in z and sometimes going in z axis not x , i think thats only my problem,

    EDIT:
    i made something good and its working perfectly but the axis made it not work good.

    can u help , please?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  6. mprimo

    mprimo

    Joined:
    Aug 1, 2018
    Posts:
    42
    I tried it before and doesn't work good
     
  7. gamesjust007

    gamesjust007

    Joined:
    Jan 3, 2017
    Posts:
    13
    Hey Realprimo,
    Did you find any solution to this issue? I have been trying this since long but am unable to do so....
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    You really have two major way to do this:

    - build checkpoints (visible or invisible), you number them increasingly and if the player touches a lower numbered checkpoint after a higher numbered, then they are going in the wrong way
    - you take the tracks direction and compare it to the player's forward vector: for example make sure that the track segments' forward vector ALWAYS direct from start to finish and your players forward vector also points forward (duh...), so when player moves you can compare the current track segment's forward vector with the player's. If they have big difference (>160 degrees for example) then the player is facing towards the start and not the finish.
    Also you can employ some timer too, so you only show alert if player goes against the flow for a little while.

    So what are the advantages/disadvantages? Many. For example you're on your own when player goes off-track.
     
    gamesjust007 likes this.
  9. gamesjust007

    gamesjust007

    Joined:
    Jan 3, 2017
    Posts:
    13
    Thanks so much for your kind & quick reply Lurking Ninja. I tried achieving this thing by Vectors and the problem that occurred was that even if the car is turning to the left or right, my system showed that he is going the wrong way but it was not the method you told I'll try that too.
    The second method you told (which is actually first) seems quick, short and accurate, will try this and will update here for people.
    Thanks again Lurking-Ninja!! :)
     
  10. saleemullah

    saleemullah

    Joined:
    Sep 9, 2018
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class positions : MonoBehaviour
    {
    public RCC_AIWaypointsContainer waypointsContainer; // Waypoints Container.
    public int currentWaypoint = 0;
    public int lap = 0;
    public int totalWaypointPassed = 0;
    public int nextWaypointPassRadius = 500;
    float againDiff, WrongWayCounter,Diff;
    public Text wrongway;
    private void Start()
    {
    if (!waypointsContainer)
    {
    waypointsContainer = FindObjectOfType(typeof(RCC_AIWaypointsContainer)) as RCC_AIWaypointsContainer;
    }
    for (int i = 0; i < waypointsContainer.waypoints.Count; i++)
    {
    GameObject waypoint = waypointsContainer.waypoints.gameObject;
    BoxCollider collider= waypoint.AddComponent<BoxCollider>();
    collider.isTrigger = true;
    collider.size = new Vector3(60,10,0);
    }
    }
    private void OnTriggerEnter(Collider other)
    {
    if (other.name== waypointsContainer.waypoints[currentWaypoint].name)
    {
    currentWaypoint++;
    PositionManager.diff = 0;
    totalWaypointPassed++;
    // If all waypoints were passed, sets the current waypoint to first waypoint and increase lap.
    if (currentWaypoint >= waypointsContainer.waypoints.Count)
    {
    currentWaypoint = 0;
    lap++;
    }
    }
    }
    void Update()
    {
    wrongwayCheck();
    if (CarRegenerate.CollideWithWall)
    {
    transform.LookAt(waypointsContainer.waypoints[currentWaypoint]);
    CarRegenerate.CollideWithWall = false;
    }
    // Next waypoint's position.
    Vector3 nextWaypointPosition = transform.InverseTransformPoint(new Vector3(waypointsContainer.waypoints[currentWaypoint].position.x, transform.position.y, waypointsContainer.waypoints[currentWaypoint].position.z));
    WrongWayCounter = nextWaypointPosition.magnitude;
    if (nextWaypointPosition.magnitude < nextWaypointPassRadius)
    {
    currentWaypoint++;
    Diff = 0;
    totalWaypointPassed++;
    // If all waypoints were passed, sets the current waypoint to first waypoint and increase lap.
    if (currentWaypoint >= waypointsContainer.waypoints.Count)
    {
    currentWaypoint = 0;
    lap++;
    }
    }
    }
    void wrongwayCheck()
    {
    if (WrongWayCounter > 100)
    {
    if (Diff < WrongWayCounter)
    {
    againDiff=Diff = WrongWayCounter;
    wrongway.text = "Wrong Way!";
    }
    else
    {
    wrongway.text = "";
    }
    if(againDiff> WrongWayCounter)
    {
    againDiff = WrongWayCounter;
    }
    else
    {
    Diff = againDiff;
    }
    }
    else
    {
    wrongway.text = "";
    }

    }

    }
     
  11. saleemullah

    saleemullah

    Joined:
    Sep 9, 2018
    Posts:
    2
    using rcc car controller just place this script on your player car and ad text to canvas for wrong way indication. It will work fine.
     
  12. MrNotoriousX7

    MrNotoriousX7

    Joined:
    Jul 25, 2018
    Posts:
    2
    I am getting an error :
    'List<Transform>' does not contain a definition for 'gameObject' and no accessible extension method 'gameObject' accepting a first argument of type 'List<Transform>' could be found (are you missing a using directive or an assembly reference?)

    on line :
    Code (CSharp):
    1. GameObject waypoint = waypointsContainer.waypoints.gameObject;
     
  13. MrNotoriousX7

    MrNotoriousX7

    Joined:
    Jul 25, 2018
    Posts:
    2
    There is a small error on the above code and I fixed it.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class WrongTurnAlert : MonoBehaviour
    7. {
    8.     public RCC_AIWaypointsContainer waypointsContainer; // Waypoints Container.
    9.     public int currentWaypoint = 0;
    10.     public int lap = 0;
    11.     public int totalWaypointPassed = 0;
    12.     public int nextWaypointPassRadius = 500;
    13.     public float againDiff, WrongWayCounter, Diff, WorngWayValue = 100;
    14.     public Text wrongway;
    15.     // Start is called before the first frame update
    16.     private void Start()
    17.     {
    18.         if (!waypointsContainer)
    19.         {
    20.             waypointsContainer = FindObjectOfType(typeof(RCC_AIWaypointsContainer)) as RCC_AIWaypointsContainer;
    21.         }
    22.         for (int i = 0; i < waypointsContainer.waypoints.Count; i++)
    23.         {
    24.             GameObject waypoint = waypointsContainer.waypoints[i].gameObject;
    25.             BoxCollider collider= waypoint.AddComponent<BoxCollider>();
    26.             collider.isTrigger = true;
    27.             collider.size = new Vector3(60,10,0);
    28.         }
    29.     }
    30.     private void OnTriggerEnter(Collider other)
    31.     {
    32.         if (other.name== waypointsContainer.waypoints[currentWaypoint].name)
    33.         {
    34.             currentWaypoint++;
    35.            
    36.             totalWaypointPassed++;
    37.             // If all waypoints were passed, sets the current waypoint to first waypoint and increase lap.
    38.             if (currentWaypoint >= waypointsContainer.waypoints.Count)
    39.             {
    40.                 currentWaypoint = 0;
    41.                 lap++;
    42.             }      
    43.         }
    44.     }
    45.     void Update()
    46.     {
    47.         wrongwayCheck();
    48.     // Next waypoint's position.
    49.         Vector3 nextWaypointPosition = transform.InverseTransformPoint(new Vector3(waypointsContainer.waypoints[currentWaypoint].position.x, transform.position.y, waypointsContainer.waypoints[currentWaypoint].position.z));
    50.         WrongWayCounter = nextWaypointPosition.magnitude;
    51.         if (nextWaypointPosition.magnitude < nextWaypointPassRadius)
    52.         {
    53.             currentWaypoint++;
    54.             Diff = 0;
    55.             totalWaypointPassed++;
    56.             // If all waypoints were passed, sets the current waypoint to first waypoint and increase lap.
    57.             if (currentWaypoint >= waypointsContainer.waypoints.Count)
    58.             {
    59.                 currentWaypoint = 0;
    60.                 lap++;
    61.             }
    62.         }
    63.     }
    64.     void wrongwayCheck()
    65.     {
    66.         if (WrongWayCounter > WorngWayValue)
    67.         {
    68.             if (Diff < WrongWayCounter)
    69.             {
    70.                 againDiff=Diff = WrongWayCounter;
    71.                 wrongway.text = "Wrong Way!";
    72.             }
    73.             else
    74.             {
    75.                 wrongway.text = "";
    76.             }
    77.             if(againDiff> WrongWayCounter)
    78.             {
    79.                 againDiff = WrongWayCounter;
    80.             }
    81.             else
    82.             {
    83.                 Diff = againDiff;
    84.             }
    85.         }
    86.         else
    87.         {
    88.         wrongway.text = "";
    89.         }
    90.  
    91.     }
    92.  
    93. }
    94.  
     
  14. Majid468

    Majid468

    Joined:
    Feb 11, 2017
    Posts:
    3
    Is it just work on the straight paths??I am using environment having sharp turns, and its showing wrong way all the time... gameplay.png .