Player Has Jittery Movement in Unity with Cinemachine Input

228 Views Asked by At

I've been working on a 3D game in Unity Game engine, and I've gotten the Freelook Camera to follow the player from behind, similar to the camera in Risk of Rain 2. Unfortunately, I can't seem to fix the jittery movement of the player.

Note that I believe that it's the player 3D rendered object that is jittering and not the camera that's following the object, as the camera doesn't seem to make the environment jitter at all, and it stays nicely centered around where the player should be.

I would imagine this is because I screwed something up in the actual unity engine, in the same vein as attaching a camera to a rigidbody and having that cause jittering. I'll attach my code and my setup below.

using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerMovement : MonoBehaviour
{
    public GameObject thirdPersonCamera;
    public float speed;
    public float jumpForce;
    public LayerMask whatIsGround;
    public float jumpCooldown;
    
    private Vector3 moveDirection;
    private float horizontalInput;
    private float verticalInput;
    private Rigidbody playerRb;
    private bool grounded;
    private bool readyToJump;
    
    // Start is called before the first frame update
    void Start()
    {
        playerRb = transform.GetChild(0).GetComponent<Rigidbody>();
    }

    private void Update()
    {
        //Horizontal and vertical mouse input with Cinemachine
        horizontalInput = thirdPersonCamera.GetComponent<CinemachineFreeLook>().m_XAxis.Value;
        verticalInput = Input.GetAxisRaw("Vertical");
        
        //Check if the player is grounded
        grounded = Physics.Raycast(transform.position, Vector3.down, 1 * 0.5f + 0.3f, whatIsGround);
        
        PlayerInputToMove();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.rotation = Quaternion.Euler(transform.rotation.x, horizontalInput, transform.rotation.z).normalized;
    }

    //Recieves input and moves the player
    private void PlayerInputToMove()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * speed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * speed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
        }

        if(Input.GetKey(KeyCode.Space) && readyToJump && grounded)
        {
            readyToJump = false;

            Jump();

            Invoke(nameof(ResetJump), jumpCooldown);
        }
    }
    
    private void Jump()
    {
        playerRb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
        Debug.Log("Called Jump()");
    }
    private void ResetJump()
    {
        readyToJump = true;
    }
}

Screenshot of my Unity environment Please note:

  1. PlayerMovement Script is attached to the 'Player' object
  2. The 3D visuals are held under the 'PlayerObject' object, along with the Rigidbody and the Capsule Collider

-Tried attaching the 'CameraPoint' object (which is a child object of 'PlayerObject') to the 'Follow' and 'Look At' boxes, as opposed to attaching the 'Player' or 'PlayerObject' object to them -Attached the 'PlayerMovement' script to 'PlayerObject' instead of 'Player' (the script is attached to 'Player' right now)

0

There are 0 best solutions below