Search Unity

Question Player jitters when moving and doesn't detect the ground with gravity on.

Discussion in 'Scripting' started by NameyMcNameFace, Mar 16, 2023.

  1. NameyMcNameFace

    NameyMcNameFace

    Joined:
    Oct 10, 2022
    Posts:
    12
    So I was following Brackey's video on youtube to create camera based movement using cinemachine, and I was able to get everything to work mostly as it should.

    Unfortunately it seems that the player is not colliding with the floor even though there is a character controller and rigidbody attached. I tried just adding a collider, but it bugs out and puts the player a few feet above the ground instead of on it.

    On top of this whenever I turn off gravity to make sure the movement works fine, the player seems to be jittering as it moves in front of the background. Setting the rigidbody to interpolate does not seem to fix the issue. If anyone has any idea for either of these I would really appreciate it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float speed = 6f;
    11.  
    12.     public float turnSmoothTime = 0.1f;
    13.     float turnSmoothVelocity;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         // Locks the Cursor to the screen once clicked on. Can get out of it by pressing escape
    19.         Cursor.lockState = CursorLockMode.Locked;
    20.     }
    21.  
    22.    
    23.     void FixedUpdate()
    24.     {
    25.         // Character movement based on camera below
    26.         float horizontal = Input.GetAxisRaw("Horizontal");
    27.         float vertical = Input.GetAxisRaw("Vertical");
    28.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    29.  
    30.         if(direction.magnitude >= 0.1f)
    31.         {
    32.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    33.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    34.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    35.  
    36.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    37.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
    38.         }
    39.     }
    40. }
    41.  
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Maybe try doing anything non-physics related in the update. Im pretty sure a characterController should be mostly handled through Update();

    Anything camera movement related, in the LateUpdate.
     
    NameyMcNameFace likes this.
  3. NameyMcNameFace

    NameyMcNameFace

    Joined:
    Oct 10, 2022
    Posts:
    12

    Thank you, that did seem to fix the jitter I was having. For some reason the character is still falling through the floor, so I'll just have to continue researching the problem there.