- Home /
Laggy FPS Camera (attached to rigid body player), I have tried everything. Unity wizards need to be summoned for this one.
EDIT: I FOUND THE SOLUTION FINALLY: The problem was with the camera rotation controlling part of the script. I was rotating the Camera as well as the Rigidbody to which the camera was attached in the mouse script. So the way to fix this was, only rotate the camera on the Y axis but not the rigid body, after that you can figure out how you want to handle the rigidbody rotations. If this doesn't make sense then please keep reading: Dani (a popular game dev youtuber) is working on a game called karslon, and he has a tutorial where he also publicly released the karslon's movement code: https://www.youtube.com/watch?v=XAC8U9-dTZU&t=126s I just downloaded the script files and compared what he did differently. For me I explained what I did wrong above, but go ahead and take a look at dani's files and see if they help.
Situation: I have an FPS character, which is a rigidbody, and the camera is attached to it.
Problem: The camera has lag/jitter, which is especially noticeable when player is moving and rotating
VIDEO OF THE PROBLEM: https://youtu.be/DQk31iOa0-Q
concentrate on the house to see how bad the lag is.
What I already understand: I know the lag is due to the rigid body character's movement being handled in the FixedUpdate function, I'm using addforce to add the character movement
What I've Tried: I tried putting the camera movement and rotation in the Update and LateUpdate. There is no noticeable difference. I've also set the rigidbody's interpolation to interpolate. It makes no difference at all and I thought for sure setting the interpolation to interpolate would make some noticeable difference atleast. It almost feels like the interpolation is not being applied. Is that possible?
Code:
Camera Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraController : MonoBehaviour
{
[SerializeField] private float lookSensitivity;
// Start is called before the first frame update
private GameObject player;
float xRotation =0.0f, yRotation = 0.0f;
private void Start()
{
player = transform.parent.gameObject;
}
// Update is called once per frame
void LateUpdate()
{
RotateCamera();
setCameraPos();
}
private void RotateCamera()
{
float mouseX = Input.GetAxisRaw("Mouse X") * lookSensitivity * Time.deltaTime;
float mouseY = Input.GetAxisRaw("Mouse Y") * lookSensitivity * Time.deltaTime;
xRotation -= mouseY;
yRotation += mouseX;
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
player.transform.localRotation = Quaternion.Euler(0f, yRotation, 0f);
}
private void setCameraPos()
{
transform.position = player.transform.position;
}
}
PlayerMovementController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementController : MonoBehaviour
{
[SerializeField] private float moveForce = 0.0f;
[SerializeField] private float jumpForce = 5.0f;
[SerializeField] private float groundCheckRay = 0.0f;
private Rigidbody playerBody;
private void Start()
{
playerBody = GetComponent<Rigidbody>();
}
private void Update()
{
Jump();
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
float hAxis = Input.GetAxisRaw("Horizontal");
float vAxis = Input.GetAxisRaw("Vertical");
Vector3 horizontalMovement = transform.right * hAxis;
Vector3 verticalMovement = transform.forward * vAxis;
Vector3 local_movement_direction = (horizontalMovement + verticalMovement).normalized;
playerBody.AddForce(local_movement_direction * moveForce * Time.fixedDeltaTime);
}
private void Jump()
{
if (isGrounded())
{
if (Input.GetKeyDown(KeyCode.Space))
{
playerBody.AddForce(0, jumpForce, 0, ForceMode.Impulse);
}
}
}
private bool isGrounded()
{
Debug.DrawRay(transform.position, Vector3.down * groundCheckRay, Color.blue);
return Physics.Raycast(transform.position, Vector3.down, groundCheckRay);
}
}
Your answer
Follow this Question
Related Questions
How do I stop my player or camera from jittering during player movement? 1 Answer
Rigidbody movement in direction of camera? 1 Answer
I'm a bit new but when my camera rotates with the player model to look around it kind of jitters 0 Answers
Camera script for ball not working as intended, looking for some tips, or advise. 0 Answers
Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer