camera jittering

Hi,

i’ve got a problem with the behaviour of my camera.
My camera has a sphere collider and a rigidbody attached as well as a script that should force the camera to follow an object.

    // ------------------------------------------------------------------------
    /// @brief Setting a flag that no collision is existing.
    // ------------------------------------------------------------------------
    public void OnCollisionExit( Collision kCollider)
    {
        m_bCollision = false;
    }

    // ------------------------------------------------------------------------
    /// @brief The damped position and rotation is calculated.
    // ------------------------------------------------------------------------
    public void FixedUpdate()
    {
        if ( m_bCollision)
        {
            return;
        }

        Vector3 kNewPosition = calculateChasePosition();
        
        // Damp the rotation around the y-axis
        float fWantedRotationAngle = Target.eulerAngles.y;
        float fCurrentRotationAngle = transform.eulerAngles.y;  	
        fCurrentRotationAngle = Mathf.LerpAngle( fCurrentRotationAngle, 
                                                 fWantedRotationAngle, 
                                                 RotationDamping * 
                                                    Time.deltaTime);
        Quaternion kCurrentRotation = 
                            Quaternion.Euler (0, fCurrentRotationAngle, 0);
        
        // Set the position of the camera to distance meters behind the target
        kNewPosition -= kCurrentRotation * Vector3.forward * HorizontalDistance;

        // Damp the height
        float fCurrentHeight = transform.position.y;
        float fWantedHeight = Target.position.y + Height;
	    fCurrentHeight = Mathf.Lerp( fCurrentHeight, 
                                     fWantedHeight, 
                                     HeightDamping * Time.deltaTime);
        kNewPosition.y = fCurrentHeight;

        rigidbody.MovePosition( kNewPosition);
        
        transform.LookAt( Target);
    }

    // ------------------------------------------------------------------------
    /// @brief Calculates the new position of the chasing camera.
    // ------------------------------------------------------------------------
    public Vector3 calculateChasePosition()
    {
        Vector3 kDesiredPosition = Target.position;

	    Vector3 kCurrentPosition = transform.position;

        Vector3 kDisplace = kCurrentPosition - kDesiredPosition;

        Vector3 kStretch = kDisplace * ( -Spring);

        Vector3 kDampenedVelocity = m_kVelocity * m_fDamping;

        Vector3 kForce = kStretch - kDampenedVelocity;

	    Vector3 kAcceleration = kForce * ( 1.0f / rigidbody.mass);

	    m_kVelocity += kAcceleration;

        Vector3 kNewPosition = kCurrentPosition + m_kVelocity;

        return kNewPosition;
    }

The problem is shown in the attached video. The script forces the camera into the wall and the collision forces the camera (obviously) in an other direction. This results in jittering.

Does someone have an idea how I can fix this problem?

Thanks a lot!

KJ

The problem is I guess because you set the camera to be updated in Update / FixedUpdate. Camera updates should be done in LateUpdate() to avoid any jitter. I guess that is the problem you have there.

Much better, thanks a lot!

Hi,

my camera now ignores collisions with the character and the camera update is done in LateUpdate. But there is still something wrong. Expecially when the camera is above the character (standing with his back to a wall) theres a jittering…

@Martin: Do you have any further ideas what could be wrong?

I attached another video file.