Search Unity

Moving a big number of cars

Discussion in 'Scripting' started by Zavalichi, May 11, 2019.

  1. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Hi guys,

    I have 300 cars on my game, but I get some bugs on moving.
    P1. When the front car is stopped, the rear car keep moving back and forth.
    P2. When the front car is stopped, the rear car sometimes stops and (problem 1) or sometimes moves over the first car.
    P3. The rotation part of the car doesn't work very good. If angle between current Point and next point is big, the car rotates instant, if I try to smooth the rotate, the car miss the next point and start rotating over that point.

    My Car:
    upload_2019-5-11_19-25-33.png

    My Code:
    Code (CSharp):
    1. private void drive()
    2.     {
    3.         transform.position += transform.forward * Time.smoothDeltaTime * CurrentSpeed;
    4.         var direction = _currentWayPoint.transform.position - transform.position;
    5.         transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(direction), _turnSpeed * Time.deltaTime);
    6.  
    7.         if (ForcedBraking || IsRed)
    8.         {
    9.             turnONBackLights();
    10.             CurrentSpeed = 0;
    11.         }      
    12.         else if (IsCarAhead)
    13.         {
    14.             turnONBackLights();
    15.             CurrentSpeed = (CurrentSpeed  < 4) ? 0 : CurrentSpeed * MaxSpeed * Time.deltaTime;
    16.         }
    17.         else if(CurrentSpeed < MaxSpeed )
    18.         {
    19.             CurrentSpeed += MaxSpeed *Time.deltaTime;
    20.             turnOFFBackLights();
    21.         }
    22.     }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You say you have 300 cars. Have you tested that two cars work? It will make debugging a lot simpler.
     
  3. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Yes, for two cars works fine. For 10 cars or more it's works weird
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    So, just to focus on your P1 (from original post above) for now. Are you saying that 9 cars in a row stopped at a red light work fine but adding a tenth car makes that tenth car move about whilst the original 9 remain stationary?
     
  5. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Yes, but not all time
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    To clarify my question, this is what I am asking:
    1. There are 8 cars stopped in a row. None of them are moving.
    2. Another car (number 9) drives up behind the 8th car and stops as expected.
    3. Another car (number 10) drives up behind the 9th car.
    So, every time you run this test, step 2 always happens as expected. But with step 3 sometimes the car stops as expected but sometimes does not.

    Is all that correct? If not, which part do I have wrong?
     
  7. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Yes, is correct.
    I changed the order and now the only problem is I have low fps fo 300 cars.
    Code (CSharp):
    1.     private void drive()
    2.     {
    3.  
    4.         if (ForcedBraking || IsRed)
    5.         {
    6.             turnONBackLights();
    7.             CurrentSpeed = 0;
    8.         }      
    9.         else if (IsCarAhead)
    10.         {
    11.             turnONBackLights();
    12.             CurrentSpeed -= (CurrentSpeed < 4) ? 0 : CurrentSpeed*(1 - MaxSpeed * Time.deltaTime);
    13.         }
    14.         else
    15.         {
    16.             if (CurrentSpeed < MaxSpeed)
    17.                 CurrentSpeed += MaxSpeed * Time.deltaTime;
    18.             //transform.position += transform.forward * Time.smoothDeltaTime * CurrentSpeed;
    19.             _rb.MovePosition(transform.position + (transform.forward * CurrentSpeed * Time.deltaTime));
    20.             var direction = _currentWayPoint.transform.position - transform.position;
    21.             transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction), _turnSpeed * Time.deltaTime);
    22.            
    23.             turnOFFBackLights();
    24.         }
    25.     }
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You will need to use the profiler to start investigating that.