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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Vehicle camera jitters.

Discussion in 'Scripting' started by Epikmemer45, Jun 22, 2022.

  1. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    I'm sorry, but I'm still having issues with jittering even though I helped it by putting the camera in lateupdate and making the car interpolate, the jitter still appears, though less than before.

    Here are my vehicle, engine sound, and camera code
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CarControlla : MonoBehaviour
    7. {
    8.     public enum Axel
    9.     {
    10.         Front,
    11.         Rear
    12.     }
    13.  
    14.     [Serializable]
    15.     public struct Wheel
    16.     {
    17.         public Transform WheelMesh;
    18.         public WheelCollider WheelCollider;
    19.         public Axel axel;
    20.     }  
    21.  
    22.     public Rigidbody arbees;
    23.     Vector3 pos;
    24.     Quaternion quat;
    25.     public float Toque, RotSpeed, maxSteerAngle;
    26.     public float addDownForceValue = 50;
    27.     public GameObject mas;
    28.     public float MPH;
    29.     public float maxSpeed = 200;
    30.     public static float Turn;
    31.     public List<Wheel> wheels;
    32.  
    33.     void Start()
    34.     {
    35.         arbees = GetComponent<Rigidbody>();
    36.         mas = GameObject.Find("mASS");
    37.         arbees.centerOfMass = mas.transform.localPosition;
    38.     }
    39.  
    40.  
    41.  
    42.     // Update is called once per frame
    43.     void FixedUpdate()
    44.     {
    45.         MoveDaCar();
    46.         SteerDaCar();
    47.     }
    48.  
    49.     private void AddDownForce()
    50.     {
    51.         arbees.AddForce(-transform.up * addDownForceValue * arbees.velocity.magnitude);
    52.     }
    53.  
    54.     private void MoveDaCar()
    55.     {
    56.         foreach (var wheel in wheels)
    57.         {
    58.             wheel.WheelCollider.GetWorldPose(out pos, out quat);
    59.             wheel.WheelMesh.position = pos;
    60.             wheel.WheelMesh.rotation = quat;
    61.         }
    62.  
    63.         foreach (var wheel in wheels)
    64.         {
    65.            wheel.WheelCollider.motorTorque = Input.GetAxis("Vertical") * Toque * 1000f * Time.deltaTime;
    66.         }
    67.  
    68.         MPH = arbees.velocity.magnitude * 15;
    69.          if (MPH > maxSpeed)
    70.          {
    71.              arbees.velocity = Vector3.ClampMagnitude(arbees.velocity, maxSpeed);
    72.          }
    73.  
    74.  
    75.         AddDownForce();
    76.     }
    77.  
    78.     private void SteerDaCar()
    79.     {
    80.         foreach(var wheel in wheels)
    81.         {
    82.             Turn = Input.GetAxis("Horizontal") * RotSpeed * 3 * Time.deltaTime;
    83.             Quaternion rotateLR = Quaternion.Euler(0f, Turn, 0f);
    84.             arbees.MoveRotation(arbees.rotation * rotateLR);
    85.  
    86.             if (wheel.axel == Axel.Front)
    87.             {
    88.                 var _steerAngle = Turn * RotSpeed * maxSteerAngle;
    89.                 wheel.WheelCollider.steerAngle = Mathf.Lerp(wheel.WheelCollider.steerAngle, _steerAngle, 0.3f);
    90.             }
    91.         }
    92.      
    93.        
    94.  
    95.     }
    96. }
    97.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraMove : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public float translateSpeed;
    9.     public float rotSpeed;
    10.  
    11.     // Update is called once per frame
    12.     void LateUpdate()
    13.     {
    14.         transform.position = Vector3.Lerp(transform.position, target.position, translateSpeed * Time.deltaTime);
    15.         transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, rotSpeed * Time.deltaTime);
    16.         transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));
    17.     }
    18.  
    19. }
    20.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EngineCarSound : MonoBehaviour
    6. {
    7.     public AudioSource EngineSound;
    8.     public Rigidbody carArbees;
    9.  
    10.     float velocityMagnitude;
    11.     float engineVolume;
    12.     float enginePitch = 0.5f;
    13.  
    14.    
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         EngineSound = GetComponent<AudioSource>();
    20.         carArbees = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void FixedUpdate()
    25.     {
    26.         EngineNoise();
    27.     }
    28.  
    29.     void EngineNoise()
    30.     {
    31.         velocityMagnitude = carArbees.velocity.magnitude;
    32.         engineVolume = velocityMagnitude * 0.05f;
    33.         engineVolume = Mathf.Clamp(engineVolume, 0.2f, 1.0f);
    34.         EngineSound.volume = Mathf.Lerp(EngineSound.volume, engineVolume, Time.deltaTime * 10);
    35.  
    36.         enginePitch = velocityMagnitude * 0.2f;
    37.         enginePitch = Mathf.Clamp(enginePitch, 0.5f, 1f);
    38.         EngineSound.pitch = Mathf.Lerp(EngineSound.pitch, enginePitch, Time.deltaTime * 1.5f);
    39.     }
    40. }
    41.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're lerping incorrectly and it's framerate dependent, any movement will cause jitter. It'll also never fully settle in place either.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Also unclear here is the object hierarchy:
    - Which object is set as "target" int he camera script?
    - Is the camera set as a child of anything?
     
  4. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    I apologize for the delay, but my camera is in an empty object called the "camera pivot", and it's following my car object.