Character jitters when Moving my camera
Hello, I'm having a problem here... so I have a third person camera set up, and my character goes in the position my camera is pointed in. However when I move my camera, the character moves with it, but in a really jittery motion. I know it has to be a problem with the camera, because when im moving without orbiting the camera around the character, the movement is smooth. here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class TPCamera : MonoBehaviour { public static TPCamera Instance; public Transform TargetLookAt; public float Distance = 5f; public float DistanceMin = 3f; public float DistanceMax = 10f; public float DistanceSmooth = 0.05f; public float X_MouseSensitivity = 5f; public float Y_MouseSensitivity = 5f; public float MouseWheelSensitivity = 5f; public float X_Smooth = 0.05f; public float Y_Smooth = 0.1f; public float Y_MinLimit = -40f; public float Y_MaxLimit = 80f; public float OcclusionDistanceStep = 0.5f; public int MaxOcclusionChecks = 10; private float mouseX = 0f; private float mouseY = 0f; private float velX = 0f; private float velY = 0f; private float velZ = 0f; private float velDistance = 0f; private float startDistance = 0f; private Vector3 position = Vector3.zero; private Vector3 desiredPosition = Vector3.zero; private float desiredDistance = 0f; void Awake() { Instance = this; } void Start() { Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax); startDistance = Distance; Reset(); } void CalculateDesiredPosition() { // evaluate distance Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, DistanceSmooth); // calculate desired position desiredPosition = CalculatePosition(mouseY, mouseX, Distance); } Vector3 CalculatePosition(float rotationX, float rotationY, float distance) { Vector3 direction = new Vector3(0, 0, -distance); Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0); return TargetLookAt.position + rotation * direction; } bool CheckIfOccluded(int count) { var isOccluded = false; var nearDistance = CheckCameraPoints(TargetLookAt.position, desiredPosition); if (nearDistance != -1) { if (count < MaxOcclusionChecks) { isOccluded = true; Distance -= OcclusionDistanceStep; if (Distance < 0.25) Distance = 0.25f; } else { Distance = nearDistance - Camera.main.nearClipPlane; desiredDistance = Distance; } } return isOccluded; } float CheckCameraPoints(Vector3 from, Vector3 to) { var nearDistance = -1f; RaycastHit hitInfo; Helper.ClipPlanePoints clipPlanePoints = Helper.ClipPlaneAtNear(to); // Draw lines in the editor to make it easier to visualize Debug.DrawLine(from, to + transform.forward * -GetComponent<Camera>().nearClipPlane, Color.red); Debug.DrawLine(from, clipPlanePoints.UpperLeft); Debug.DrawLine(from, clipPlanePoints.LowerLeft); Debug.DrawLine(from, clipPlanePoints.UpperRight); Debug.DrawLine(from, clipPlanePoints.LowerRight); Debug.DrawLine(clipPlanePoints.UpperLeft, clipPlanePoints.UpperRight); Debug.DrawLine(clipPlanePoints.UpperRight, clipPlanePoints.LowerRight); Debug.DrawLine(clipPlanePoints.LowerRight, clipPlanePoints.LowerLeft); Debug.DrawLine(clipPlanePoints.LowerLeft, clipPlanePoints.UpperLeft); if (Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player") nearDistance = hitInfo.distance; if (Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player") if(hitInfo.distance < nearDistance || nearDistance == -1) nearDistance = hitInfo.distance; if (Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player") if (hitInfo.distance < nearDistance || nearDistance == -1) nearDistance = hitInfo.distance; if (Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player") if (hitInfo.distance < nearDistance || nearDistance == -1) nearDistance = hitInfo.distance; if (Physics.Linecast(from, to + transform.forward * -GetComponent<Camera>().nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player") if (hitInfo.distance < nearDistance || nearDistance == -1) nearDistance = hitInfo.distance; return nearDistance; } void UpdatePosition() { var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth); var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth); var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth); position = new Vector3(posX, posY, posZ); transform.position = position; transform.LookAt(TargetLookAt); } void LateUpdate() { if (TargetLookAt == null) return; HandlePlayerInput(); var count = 0; do { CalculateDesiredPosition(); count++; } while (CheckIfOccluded(count)); UpdatePosition(); } void HandlePlayerInput() { var deadZone = 0.1f; if (Input.GetMouseButton(1)) { // The RMB is DOWN get mouse Axis input mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity; mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity; } // This is where we will limit or clamp mouseY mouseY = Helper.ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit); if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone) { desiredDistance = Mathf.Clamp(Distance - Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity, DistanceMin, DistanceMax); } } public void Reset() { mouseX = 0; mouseY = 10; Distance = startDistance; desiredDistance = Distance; } }
Your answer
![](https://koobas.hobune.stream/wayback/20220612200735im_/https://answers.unity.com/themes/thub/images/avi.jpg)