Question by
Gaurav_13 · Oct 24, 2021 at 12:58 PM ·
rigidbodycamera-movementcamera rotatecamera-lookcamera follow
getting Jittery movement on camera when player rotating and moving in same time
Hello all, I'm trying to create a smooth camera follow script but I don't know why I'm getting "jittery" movement....even I'm using "FixedUpdate" in PlayerMovement.cs script for player movement sach as rotation and movement, and the "LateUpdate" in camera follow script and also I set player "RigidBody" to "Interpolate".. Any Help appreciated thank you.
these are my scripts:
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
private Transform playerTransform = null;
[Range(1, 10)]
[SerializeField] private float smoothSpeed = 5f;
[SerializeField] private Vector3 offset;
[SerializeField] private Space offsetPositionSpace = Space.Self;
[SerializeField] private bool lookAt = true;
private void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void LateUpdate()
{
if (offsetPositionSpace == Space.Self)
{
Vector3 desiredPos = playerTransform.TransformPoint(offset);
Vector3 smoothPos = Vector3.Lerp(
transform.position,
desiredPos,
smoothSpeed * Time.deltaTime);
transform.position = smoothPos;
}
if (lookAt)
{
transform.LookAt(playerTransform);
}
else
{
transform.rotation = playerTransform.rotation;
}
}
}
Blockquote
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody rb;
[SerializeField] private float speed = 50f;
[SerializeField] private float mouseSensitivity = 100f;
[SerializeField] private Transform playerBody;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
ProcessInput();
ProcessRotation();
}
void ProcessInput()
{
if (Input.GetAxis("Horizontal") > 0)
{
rb.AddRelativeForce(Vector3.right * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);
}
else if (Input.GetAxis("Horizontal") < 0)
{
rb.AddRelativeForce(Vector3.left * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);
}
else if (Input.GetAxis("Vertical") > 0)
{
rb.AddRelativeForce(Vector3.forward * speed * Time.fixedDeltaTime,
ForceMode.VelocityChange);
}
else if (Input.GetAxis("Vertical") < 0)
{
rb.AddRelativeForce(Vector3.back * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);
}
}
void ProcessRotation()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.fixedDeltaTime;
playerBody.Rotate(Vector3.up * mouseX);
}
}
Comment