Camera Scripting Problems

Hello, I happen to be working on a simple kart racing demo, but I’ve come to a bit of a problem. While my Cars’ controller script work properly enough, It doesn’t seem to work well with the other script I made for my camera. You see, I based both my scripts each from two different sources. You can check out the sources yourself in the links below.

Arcade Style Bouncy Vehicle tutorial (Car object)

SmoothFollow (Camera)

Regardless, here’s how it all goes down. When the camera is parented to the Care object, the view works fine, but here’s the thing though. the game view of the Car is always static, even when the car turns, the camera always faces the back of the object. Now here’s where the cameras’ script come into play. Basically how it works is that it allows a camera to follow an assigned object more smoothly. However when I gave my camera said script, It’s does what it’s supposed to do, but the object its’ assigned to follow seems to “twitch” when it moves. Even when object stops moving, it still seems to glitch and shake. I’m a little bit confused on how to alleviate this little issue myself, so I’d really appreciate the help. Of course, you can always go to the links provided in this comment. I’ve also posted sample code of my scripts for you to examine and try out for yourselves.

Car

using UnityEngine;
using System.Collections;

public class CarController : MonoBehaviour
{
    Rigidbody body;
    float deadZone = 0.1f;
    public float m_groundedDrag = 3f;
    public float maxVelocity = 50;
    public float hoverForce = 1000;
    public float gravityForce = 1000f;
    public float hoverHeight = 1.5f;
    public GameObject[] hoverPoints;
    public float forwardAcceleration = 8000f;
    public float reverseAcceleration = 4000f;
    float thrust = 0f;
    public float turnStrength = 1000f;
    float turnValue = 0f;
    public ParticleSystem[] dustTrails = new ParticleSystem[2];
    int layerMask;
   
    void Start()
      {
        body = GetComponent<Rigidbody>();
        body.centerOfMass = Vector3.down;
    
        layerMask = 1 << LayerMask.NameToLayer("Vehicle");
        layerMask = ~layerMask;
      }
   
    // Uncomment this to see a visual indication of the raycast hit points in the editor window
//  void OnDrawGizmos()
//  {
//
//    RaycastHit hit;
//    for (int i = 0; i < hoverPoints.Length; i++)
//    {
//      var hoverPoint = hoverPoints [i];
//      if (Physics.Raycast(hoverPoint.transform.position,
//                          -Vector3.up, out hit,
//                          hoverHeight,
//                          layerMask))
//      {
//        Gizmos.color = Color.blue;
//        Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
//        Gizmos.DrawSphere(hit.point, 0.5f);
//      } else
//      {
//        Gizmos.color = Color.red;
//        Gizmos.DrawLine(hoverPoint.transform.position,
//                       hoverPoint.transform.position - Vector3.up * hoverHeight);
//      }
//    }
//  }
   
    void Update()
      {
        // Main Thrust
        thrust = 0.0f;
        float acceleration = Input.GetAxis("Vertical");
        if (acceleration > deadZone)
            thrust = acceleration * forwardAcceleration;
        else if (acceleration < -deadZone)
            thrust = acceleration * reverseAcceleration;
    
        // Turning
        turnValue = 0.0f;
        float turnAxis = Input.GetAxis("Horizontal");
        if (Mathf.Abs(turnAxis) > deadZone)
            turnValue = turnAxis;
      }
   
    void FixedUpdate()
  {

    //  Do hover/bounce force
        RaycastHit hit;
        bool  grounded = false;
        for (int i = 0; i < hoverPoints.Length; i++)
        {
              var hoverPoint = hoverPoints [i];
            if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hit,hoverHeight, layerMask))
            {
                body.AddForceAtPosition(Vector3.up * hoverForce* (1.0f - (hit.distance / hoverHeight)), hoverPoint.transform.position);
                grounded = true;
            }
              else
              {
                // Self levelling - returns the vehicle to horizontal when not grounded and simulates gravity
                if (transform.position.y > hoverPoint.transform.position.y)
                {
                    body.AddForceAtPosition(hoverPoint.transform.up * gravityForce, hoverPoint.transform.position);
                }
                else
                {
                    body.AddForceAtPosition(hoverPoint.transform.up * -gravityForce, hoverPoint.transform.position);
                }
              }
        }
           
        var emissionRate = 0;
        if(grounded)
        {
            body.drag = m_groundedDrag;
            emissionRate = 10;
        }
        else
        {
            body.drag = 0.1f;
            thrust /= 100f;
            turnValue /= 100f;
        }

       

    // Handle Forward and Reverse forces
    if (Mathf.Abs(thrust) > 0)
      body.AddForce(transform.forward * thrust);

    // Handle Turn forces
    if (turnValue > 0)
    {
      body.AddRelativeTorque(Vector3.up * turnValue * turnStrength);
    } else if (turnValue < 0)
    {
      body.AddRelativeTorque(Vector3.up * turnValue * turnStrength);
    }

    // Limit max velocity
    if(body.velocity.sqrMagnitude > (body.velocity.normalized * maxVelocity).sqrMagnitude)
    {
        body.velocity = body.velocity.normalized * maxVelocity;
    }
  }
}

Camera

using UnityEngine;
using System.Collections;

public class CameraSmoothFollow : MonoBehaviour {

    public Transform target;
    // Use this for initialization
    public float distance;
    public float height;
    public float damping;
    public bool smoothRotation;
    public bool followBehind;
    public float rotationDamping;
   
    // Update is called once per frame
    void Update () {
        Vector3 wantedPosition;
        if(followBehind)
            wantedPosition = target.TransformPoint(0, height, -distance);
        else
            wantedPosition = target.TransformPoint(0, height, distance);
       
        transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
       
        if (smoothRotation) {
            Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
            transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
        }
        else transform.LookAt (target, target.up);
    }
}

Don’t parent camera to car, if you use the follow script.

but this might look nicer, (bit complicated to setup at first, but play with the example scenes)
Cinemachine BaseRig (free)

My Camera isn’t parented to my car, but it object still twitchs in shape as it moves when followed by camera itself. I’ll still try out the Cinemachine BaseRig though, to see how it works for my project. Who knows, I might like it.