Search Unity

How Can I Fix This?

Discussion in 'Scripting' started by MuhammetUgurBozdemir, Apr 10, 2021.

  1. MuhammetUgurBozdemir

    MuhammetUgurBozdemir

    Joined:
    Mar 6, 2021
    Posts:
    45
    When my car reach the max speed its start lagging.How can I fix this?
    Code (CSharp):
    1.   public float moveSpeed = 5f;
    2.     public float maxSpeed = 60f;
    3.     public float minSpeed = 5f;
    4.  
    5.  
    6.     void Start()
    7.     {
    8.  
    9.     }
    10.  
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetKey(KeyCode.W))
    15.         {
    16.             car.rotation = Quaternion.Lerp(car.rotation, Quaternion.Euler(-5, 0, 0), 0.03f);
    17.          
    18.             moveSpeed += 0.1f;
    19.             if (moveSpeed > maxSpeed)
    20.             {
    21.                 moveSpeed = maxSpeed;
    22.             }
    23.      }
    24.   private void FixedUpdate()
    25.     {
    26.  
    27.         transform.Translate(0, 0, 1 * moveSpeed * Time.deltaTime, Space.World);
    28.  
    29.  
    30.  
    31.     }
    32.      
    33.         }
     
  2. Blejz43

    Blejz43

    Joined:
    Mar 4, 2020
    Posts:
    8
    try putting the whole code from Update into FixedUpdate. This always workes for me