Help with player ridgidbody

So after trying the course using a character controller, This simply wont work for the actions i need the character to perform but I am having trouble with this.

The player does not roatate at the look roation properly,either jitters or simply does not look at all.
The player will jitter crazy when moving with both A & D at the same time.
Also when I run the player will Jitter,

It Seems like the movment is not calculating correct, I have even tried just pasing in the motion to no avail.

stateMachine.RB.MovePosition(motion * deltaTime );

Here is how I Setup the ridgidbody

Here is my methods; if anyone could help with this would be great.

PlayerBaseState.Cs

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public abstract class PlayerBaseState : State
{
    protected PlayerStateMachine stateMachine;

    public PlayerBaseState(PlayerStateMachine stateMachine)
    {
        this.stateMachine = stateMachine;
    }

    protected void Move(Vector3 motion,float deltaTime)
    {
        Vector3 newPos = stateMachine.RB.position + motion * deltaTime;
         stateMachine.RB.MovePosition(newPos);
    }
}


PlayerFreeLookState.CS

using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine.PostFX;
using UnityEngine;

public class PlayerFreeLookState : PlayerBaseState
{
    private readonly int FreeLookSpeedHash = Animator.StringToHash("FreeLookSpeed");
    private readonly int FreeBlendHash = Animator.StringToHash("FreeLookBlendTree");
    private const float AnimatorDampTime = 0.1f;


    private bool isSprinting = false;
    private bool haStamina = true;

    public PlayerFreeLookState(PlayerStateMachine stateMachine) : base(stateMachine) { }

    public override void Enter()
    {      
        stateMachine.isInWater = false;
        if(!stateMachine.isInWater)
        {
            stateMachine.InputReader.SprintEvent += HandleSprint;
            stateMachine.InputReader.SprintEndEvent += StopSprint;
            stateMachine.Animator.Play(FreeBlendHash); 

            Debug.Log("Entering FreelookState");
        }


    }

    public override void Tick(float deltaTime)
    {
        Vector3 movement = CalculateMovement();

        UpdateStaminaStatus();
        Move(movement * GetCurrentMoveSpeed(), deltaTime);

        if (stateMachine.InputReader.MovementValue == Vector2.zero)
        {
            stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0f, AnimatorDampTime, deltaTime);
            RecoverStmina(deltaTime);
            return;
        }
        else
        {
            // There is movement input
            if (isSprinting && haStamina )
            {
                // Sprinting
                stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1f, AnimatorDampTime, deltaTime);
                DrainStamina(deltaTime);
            }
            else
            {
                // Walking
                stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0.5f, AnimatorDampTime, deltaTime);
                RecoverStmina(deltaTime);
            }
        }
        FaceMovementDirection(movement, deltaTime);

    }



    public override void Exit()
    {
        stateMachine.InputReader.SprintEvent -= HandleSprint;
        stateMachine.InputReader.SprintEndEvent -= StopSprint;
        
        Debug.Log("Exiting FreelookState");
    }
    public override void OnTriggerEnter(Collider other)
    {
        if (!stateMachine.isInWater && other.CompareTag("Water"))
        {
            stateMachine.isInWater = true;
            if (stateMachine.isInWater)
            {
                stateMachine.SwitchState(new PlayerFloatingState(stateMachine));
            }
            /*
            CinemachineVolumeSettings volumeSettings = stateMachine.cinemachineFreeLook.GetComponentInChildren<CinemachineVolumeSettings>();
            if (volumeSettings != null)
            {
                volumeSettings.enabled = true;
                Debug.Log("CinemachineVolumeSettings found.");
            }
            RenderSettings.fog = true;
            stateMachine.SwitchState(new PlayerSwimState(stateMachine));
            */
        }
    }

    // move to playerstatemachine

    private Vector3 CalculateMovement()
    {
        Vector3 cameraForward = stateMachine.MainCameraTransform.forward;
        Vector3 cameraRight = stateMachine.MainCameraTransform.right;

        cameraForward.y = 0;
        cameraRight.y = 0;

        cameraForward.Normalize();
        cameraRight.Normalize();

        return cameraForward * stateMachine.InputReader.MovementValue.y +
        cameraRight * stateMachine.InputReader.MovementValue.x;
    }

    private void FaceMovementDirection(Vector3 movement, float deltaTime)
    {
        stateMachine.transform.rotation =  Quaternion.Lerp(
            stateMachine.transform.rotation,
            Quaternion.LookRotation(movement),
            deltaTime * stateMachine.RotationDamping);

    }

    private void HandleSprint()
    {
        isSprinting = true;

    }
    private void StopSprint()
    {
        isSprinting = false;
    }

    private float  GetCurrentMoveSpeed()
    {
        if(isSprinting && haStamina)
        {
            return stateMachine.RunMovementSpeed;
        }
        else
        {
            return stateMachine.WalkMovementSpeed;
        }
    }

    private void UpdateStaminaStatus()
    {
        haStamina = stateMachine.currentStamina > 0;
    }
        private void DrainStamina(float deltaTime)
    {
        stateMachine.currentStamina -= stateMachine.StaminaDrainSpeed * deltaTime;
        stateMachine.currentStamina = Mathf.Clamp(stateMachine.currentStamina, 0f, stateMachine.maxStamina);
    }

    private void RecoverStmina(float deltaTime)
    {
        stateMachine.currentStamina += stateMachine.StaminaRecoverSpeed * deltaTime;
        stateMachine.currentStamina = Mathf.Clamp(stateMachine.currentStamina, 0f, stateMachine.maxStamina);
    }


}


Hi LaniganDev,

In which course and lecture are you? If this is a personal project and if you still need help, please feel free to ask our helpful community of students for advice over on our Discord chat server. :slight_smile:

Privacy & Terms