I'm currently working on an FPS Game in Unity and I created the Player/Camera Movement but now when I move the Camera in playmode it's very jerky/jittery.
That's what I have already done:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
private void Start(){
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void Update(){
MyInput();
}
private void FixedUpdate(){
MovePlayer();
}
private void MyInput(){
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
}
private void MovePlayer(){
// calculate movement direction
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCam : MonoBehaviour
{
public float sensX;
public float sensY;
public Transform orientation;
float xRotation;
float yRotation;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update(){
// get mouse input
float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// rotate cam and orientation
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
orientation.rotation = Quaternion.Euler(0, yRotation, 0);
}
}
using UnityEngine;
public class MoveCamera : MonoBehaviour {
public Transform player;
void Update() {
transform.position = player.transform.position;
}
}
Does anyone know how to fix this problem? I would really appreciate it. If you need more information just ask.
I really don't know what to do.