Search Unity

Third Person Camera - Small Jitter Problem

Discussion in 'Scripting' started by Deleted User, Oct 8, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    I'm making a third person camera with collision ( 3d platformer inspired - so no mouse ), the movement is smooth enough when I am free roaming about a level, but if I get near a wall, the collision code kicks in, and my movement becomes a little jittery, I'm aware that this is a problem probably caused by my SmoothFactor, because if I make it 1 instead of 0.1, the jittery issue disappears, wondering how I might be able to fix this small issue, before I tackle other things in my camera script ?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // Player Camera Controller
    6.  
    7. public class PlayerCameraFix : MonoBehaviour
    8. {
    9.     [Header("Camera Target")]
    10.     public Transform playerReference; // Public Reference to Player
    11.  
    12.     [Header("Camera Attributes - Look At")]
    13.     public bool lookAtPlayer = true; // Boolean ( Look At Player - True / False )
    14.  
    15.     [Header("Camera Attributes - Smooth Follow")]
    16.     [Range(0.1f, 1.0f)]
    17.     public float smoothFactor = 0.1f; // Camera Smoothness
    18.  
    19.     [Header("Camera Attributes - Offset")]
    20.     public bool useCameraOffset = false; // Boolean ( Use Camera Offset - True / False )
    21.     public Vector3 cameraOffset; // Set The Camera Default Position if useCameraOffset = true
    22.  
    23.     [Header("Camera Attributes - Collision")]
    24.     public LayerMask layerMask; // LayerMask ( To choose where collisions occur )
    25.     [Tooltip("Minimum Collision Distance")] // Minimum Collision Distance
    26.     [Range(0.1f, 1.0f)]
    27.     public float minimumDistance = 0.2f;
    28.  
    29.     private void Start()
    30.     {
    31.         if (useCameraOffset) // Get Camera Offset values if Boolean useCameraOffset is true
    32.         {
    33.             transform.position = playerReference.position + cameraOffset;
    34.         }
    35.         else // Just use the cameras current position
    36.         {
    37.             cameraOffset = transform.position - playerReference.position;
    38.         }
    39.     }
    40.  
    41.     private void LateUpdate()
    42.     {
    43.         // Set New Position for Camera
    44.         Vector3 newPosition = playerReference.position + cameraOffset;
    45.  
    46.         // Transform Camera position to New Position
    47.         transform.position = Vector3.Slerp(transform.position, newPosition, smoothFactor);
    48.  
    49.         // Camera Collision - Debug.Drawline for visual feedback in editor
    50.         Debug.DrawLine(transform.position, playerReference.position, Color.red);
    51.         RaycastHit hit;
    52.  
    53.         // Only test for what is in the chosen LayerMask
    54.         if (Physics.Linecast(playerReference.position, transform.position, out hit, layerMask))
    55.         {
    56.             // Minimum Distance to offset Camera from walls / obstacles to minimise clipping
    57.             Vector3 hitPoint = new Vector3(hit.point.x + hit.normal.x * minimumDistance, hit.point.y, hit.point.z + hit.normal.z * minimumDistance);
    58.  
    59.             // Update Camera position
    60.             transform.position = new Vector3(hitPoint.x, transform.position.y, hitPoint.z);
    61.         }
    62.  
    63.         // If Boolean lookAtPlayer is true
    64.         if (lookAtPlayer)
    65.         {
    66.             // Look At Player
    67.             transform.LookAt(playerReference);
    68.         }
    69.     }
    70. }
     
    Last edited by a moderator: Oct 8, 2018
  2. Deleted User

    Deleted User

    Guest

    Ok, had a brain fart, I just set to the SmoothFactor to 1 when collision occurs otherwise just set it back to the default value, seems to work fine ! :rolleyes:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // Player Camera Controller
    6.  
    7. public class PlayerCameraFix : MonoBehaviour
    8. {
    9.     [Header("Camera Target")]
    10.     public Transform playerReference; // Public Reference to Player
    11.  
    12.     [Header("Camera Attributes - Look At")]
    13.     public bool lookAtPlayer = true; // Boolean ( Look At Player - True / False )
    14.  
    15.     [Header("Camera Attributes - Smooth Follow")]
    16.     [Range(0.1f, 1.0f)]
    17.     public float smoothFactor = 0.1f; // Camera Smoothness
    18.     private float smoothFactorValue; // Stores our default smoothFactor value
    19.  
    20.     [Header("Camera Attributes - Offset")]
    21.     public bool useCameraOffset = false; // Boolean ( Use Camera Offset - True / False )
    22.     public Vector3 cameraOffset; // Set The Camera Default Position if useCameraOffset = true
    23.  
    24.     [Header("Camera Attributes - Collision")]
    25.     public LayerMask layerMask; // LayerMask ( To choose where collisions occur )
    26.     [Tooltip("Minimum Collision Distance")] // Minimum Collision Distance
    27.     [Range(0.1f, 1.0f)]
    28.     public float minimumDistance = 0.2f;
    29.  
    30.     private void Start()
    31.     {
    32.         // Get our smoothFactor value on Start
    33.         smoothFactorValue = smoothFactor;
    34.  
    35.         if (useCameraOffset) // Get Camera Offset values if Boolean useCameraOffset is true
    36.         {
    37.             transform.position = playerReference.position + cameraOffset;
    38.         }
    39.         else // Just use the cameras current position
    40.         {
    41.             cameraOffset = transform.position - playerReference.position;
    42.         }
    43.     }
    44.  
    45.     private void LateUpdate()
    46.     {
    47.         // Set New Position for Camera
    48.         Vector3 newPosition = playerReference.position + cameraOffset;
    49.  
    50.         // Transform Camera position to New Position
    51.         transform.position = Vector3.Slerp(transform.position, newPosition, smoothFactor);
    52.  
    53.         // Camera Collision - Debug.Drawline for visual feedback in editor
    54.         Debug.DrawLine(transform.position, playerReference.position, Color.red);
    55.         RaycastHit hit;
    56.  
    57.         // Only test for what is in the chosen LayerMask
    58.         if (Physics.Linecast(playerReference.position, transform.position, out hit, layerMask))
    59.         {
    60.             // To avoid Jitter - Set smoothFactor to 1
    61.             smoothFactor = 1.0f;
    62.  
    63.             // Minimum Distance to offset Camera from walls / obstacles to minimise clipping
    64.             Vector3 hitPoint = new Vector3(hit.point.x + hit.normal.x * minimumDistance, hit.point.y, hit.point.z + hit.normal.z * minimumDistance);
    65.  
    66.             // Update Camera position
    67.             transform.position = new Vector3(hitPoint.x, transform.position.y, hitPoint.z);
    68.         }
    69.         else // Set our smoothFactor back to user user chosen value
    70.         {
    71.             smoothFactor = smoothFactorValue;
    72.         }
    73.  
    74.         // If Boolean lookAtPlayer is true
    75.         if (lookAtPlayer)
    76.         {
    77.             // Look At Player
    78.             transform.LookAt(playerReference);
    79.         }
    80.     }
    81. }