Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with simple traffic script

Discussion in 'Scripting' started by S3M1CZ, Dec 31, 2019.

  1. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    [SOLVED]
    Skip to the underlined header below.

    Greetings, I'm currently working on a small traffic script, which moves cars through waypoints.

    The idea was simple in my head:
    - add nearby waypoints to list (triggering nearby waypoints adds them)
    - remove waypoints that are too far or on same position as car's last position
    - from remaining positions pick one random position
    - move to the random position and repeat

    I feel like I'm missing something obvious, but no matter what I've tried, the script never worked properly. My best result was successful movement across the road network, but the "remove last position from list" system didn't worked, so the car moved awkwardly.

    MY SOLUTION

    I've completely changed the entire idea and code, so that the car has a child gameobject with trigger that acts as a vision of the car. If the trigger detects a waypoint, it moves towards it, otherwise it will rotate until it finds one.

    I'm still learning Unity, so my solution might not be the best (make sure the car isn't moving too fast), but in case you would like to accomplish the same thing, then here it is.

    All you have to do is to...
    - create a Road object with a WayPoint child (tag + trigger)
    - create a Car object with a Vision child (make sure the trigger of the child can detect nearby WayPoints but won't detect WayPoints that are too far away or WayPoint that the car stands on (decrease the trigger's X offset by a little bit, -0.5 worked for me)
    - make sure they will trigger properly and add this code to the Vision child
    Code (CSharp):
    1. [SerializeField] Vector3 nextWayPoint;
    2.     [SerializeField] float carSpeed = 0.01f;
    3.     [SerializeField] bool isMoving = false;
    4.     [SerializeField] bool isEmpty = true;
    5.  
    6.     void Update()
    7.     {
    8.         if(isEmpty == true)
    9.         {
    10.             transform.Rotate(0, 0, transform.rotation.z - 90);
    11.         }
    12.  
    13.         if(transform.position != nextWayPoint && nextWayPoint != new Vector3(0, 0, 0))
    14.         {
    15.             transform.parent.transform.position = Vector3.MoveTowards(transform.position, nextWayPoint, carSpeed);
    16.             isMoving = true;
    17.         } else
    18.         {
    19.             isMoving = false;
    20.         }
    21.     }
    22.  
    23.     private void OnTriggerEnter2D(Collider2D collision)
    24.     {
    25.         if(isMoving == false)
    26.         {
    27.             isEmpty = false;
    28.  
    29.             if(collision.tag == "WayPoint")
    30.             {
    31.                 nextWayPoint = collision.transform.position;
    32.             }
    33.         }
    34.     }
    35.  
    36.     private void OnTriggerExit2D(Collider2D collision)
    37.     {
    38.         if(collision.tag == "WayPoint")
    39.         {
    40.             isEmpty = true;
    41.         }
    42.     }
     
    Last edited: Dec 31, 2019
  2. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    I don't see any problems with those methods so I guess it has to be somethign about how you call them.

    On a side note: it's usually a good idea to include Time.deltaTime into the speed parameter when using MoveTowards (). Not sure if you've done that already, but that might also cause some jittering.
     
    S3M1CZ likes this.
  3. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    Thank you for replying! I gave up on the previous code and changed the idea, so that the same thing will be accomplished by different means and fortunately it works. Updated the post for details.