The problem is that rb.velocity probably returns too small values
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementController : MonoBehaviour
{
public float moveSpeed = 5f;
public float sprintSpeed = 10f;
public float crouchSpeed = 2.5f;
public float jumpForce = 2f;
public float rotationSpeed = 200f;
public float dashSpeed = 100f;
public float dashCooldown = 12f;
public bool isGrounded;
public bool isJumpAllowed = false;
public KeyCode dashKey = KeyCode.Q;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
private Rigidbody rb;
private Vector3 velocity;
private bool isJumping = false;
private bool isCrouching;
private bool isSprinting;
[SerializeField] private bool isDashing;
[SerializeField] private float lastDashTime = 0f;
private Player player = null;
public void Init(Player pPlayer)
{
player = pPlayer;
}
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
lastDashTime = -dashCooldown;
}
// Update is called once per frame
public void UpdatePMC()
{
if (Input.GetButtonDown("Jump") && isGrounded && isJumpAllowed)
{
rb.AddForce(new Vector3(0f, jumpForce, 0f), ForceMode.VelocityChange);
isJumping = true;
} else {
isJumping = false;
}
if ((Input.GetKeyDown(dashKey) && isGrounded) && Time.time - lastDashTime >= dashCooldown)
{
isDashing = true;
lastDashTime = Time.time;
}
playerAnim();
}
public void FixedUpdatePMC()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = (transform.right * x + transform.forward * z).normalized;
if (Input.GetKey(KeyCode.LeftShift))
{
rb.MovePosition(transform.position + move * sprintSpeed * Time.fixedDeltaTime);
isSprinting = true;
}
else if (Input.GetKey(KeyCode.C))
{
rb.MovePosition(transform.position + move * crouchSpeed * Time.fixedDeltaTime);
isCrouching = true;
}
else if (isDashing)
{
Vector3 dashDirection = transform.forward;
RaycastHit dashHit;
if (Physics.Raycast(transform.position, dashDirection, out dashHit, dashSpeed * Time.fixedDeltaTime))
{
rb.MovePosition(dashHit.point);
}
else
{
rb.MovePosition(transform.position + dashDirection + dashDirection * dashSpeed * Time.fixedDeltaTime);
//rb.MovePosition(transform.position + move + dashDirection * dashSpeed * Time.fixedDeltaTime);
}
isDashing = false;
} else if (isJumping)
{
}
else
{
rb.MovePosition(transform.position + move * moveSpeed * Time.fixedDeltaTime);
isSprinting = false;
isCrouching = false;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100f, groundMask))
{
Vector3 targetPosition = hit.point;
targetPosition.y = transform.position.y;
Vector3 direction = (targetPosition - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.fixedDeltaTime * rotationSpeed);
}
}
private void playerAnim()
{
if (isGrounded)
{
Debug.Log("Player is moving: " + rb.velocity);
if (rb.velocity != Vector3.zero)
{
if (isSprinting)
{
player.AnimController.SetMovementState(MovingState.Running);
}
else
{
player.AnimController.SetMovementState(MovingState.Walking);
}
}
else
{
player.AnimController.SetMovementState(MovingState.Idle);
}
}
else
{
player.AnimController.SetMovementState(MovingState.Idle);
}
}
}
I also tried comparing with sqrMagnitude but the result is basically the same.
If I'm comparing velicity with vector. Sometimes the animation switches for a split second, because even if the player is not moving, there is still some movement.