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

Camera Movement (Lagging follow Car)

Discussion in 'Scripting' started by jackvob1, Feb 5, 2020.

  1. jackvob1

    jackvob1

    Joined:
    Mar 2, 2018
    Posts:
    38
    Hello,
    my main goal is for the camera run smoothly looking at the payer car and AI car, I'm stuck so I've tried using FixedUpdate (The player Car is lagging but AI car run smooth), Update (The player Car run smooth but AI car is lagging), LateUpdate (The player Car run smooth but AI car is lagging) i anyone know how to make both car run smooth ?
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    You need to provide more context or code.

    My blind guess is that you should separate camera movement from car movement, something like camera "following" player position. You would update car, AI or whaterver in FixedUpdate and then Camera would update in LateUpdate.
    There are great tutorials on this site here it might be helpful. Have in mind, that if you use rigidbody to move your cars, you should use apply force, move position etc. only in FixedUpdate, otherwise you might get strage results.
     
  3. jackvob1

    jackvob1

    Joined:
    Mar 2, 2018
    Posts:
    38
    there's a couple of limitation for me because i use free asset with lock source code. So i want to create a drag race car between player and AI all run correctly first i try to play it the AI car is lagging but the player car its not lagging and i try use this code for the camera below

    void FixedUpdate()
    {
    Vector3 dPos = target.position + dist;
    Vector3 sPos = Vector3.Lerp(transform.position, dPos, cameraSpeed * Time.deltaTime);
    transform.position = sPos;
    transform.LookAt(lookTarget.position);
    }
    }

    and the problem change now the player car its lagging and the AI car run smoothly.
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    First read this.
    It's hard to help you this way - what does that mean lagging? It loses frames? It teleports? Stops movement sometimes?
    I do not know what asset you use, but if you can't make smooth camera movement or whatever the problem is, then it would mean it's bad asset - most likely you just did something wrong.

    If you want to be sure, that camera doesn't jitter it must be updated every frame. It means you must do it in Update or LateUpdate. If your car uses rigidbody (mostlikely true) then it need to be updated in FixedUpdate, so it's synchronized with physics. Possibly it's not about the camera, but AI itself.

    With such little information, that is all I can personally say.