So I’m struggling with my bool of isSprinting and the animation connection between the two. I’ve been furiously attempting everything videos have taught me and nothing is working. Can anyone help out? Here is the entire script
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class PlayerAction : MonoBehaviour
{
public float walkSpeed = 2f;
public float runSpeed = 4f;
public float rotationSpeed = 0.15f;
public float jumpForce = 5f;
private bool isGrounded;
private bool jumpRequested;
private Rigidbody rb;
private Vector3 moveInput;
private Animator animator;
private Player_controls controls;
private static readonly int State = Animator.StringToHash("State");
private static readonly int jump = Animator.StringToHash("Jump");
private static readonly int Run = Animator.StringToHash("Run");
public bool isSprinting;
private enum PlayerState
{
Idle = 0,
Walk = 1,
Run = 2
}
private void Awake()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
controls = new Player_controls();
controls.Player.SprintS.performed += x => SprintPressed();
controls.Player.SprintF.performed += x => SprintReleased();
}
private void SprintPressed()
{
isSprinting = true;
}
private void SprintReleased()
{
isSprinting = false;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Ground checking is done on Update to be preformed per frame
// Cast a ray downward to check if close to ground
Vector3 rayOrgin = transform.position + Vector3.up * 0.1f;
float rayDistance = 2f;
if (Physics.Raycast(transform.position, Vector3.down, out var hit, rayDistance))
{
if (hit.collider.CompareTag("Ground"))
{
isGrounded = true;
}
}
//check id jump is triggerd.
// if jump is not trigger, request for jump to be preformed.
if (controls.Player.Jump.triggered && isGrounded)
{
jumpRequested = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
// read input device value
Vector2 input = controls.Player.Move.ReadValue<Vector2>();
moveInput = new Vector3(input.x, 0f, input.y);
// move the player using Rigid body
if (isSprinting == true)
{
rb.MovePosition(rb.position + moveInput * (runSpeed * Time.deltaTime));
SetAnimationState(PlayerState.Run);
if (moveInput.magnitude >= 0.1f)
{
// change player rotation to face the movement direction.
Quaternion newRotation = Quaternion.LookRotation(moveInput, Vector3.up);
rb.rotation = Quaternion.Slerp(rb.rotation, newRotation, rotationSpeed);
// set the animation State to Running.
SetAnimationState(PlayerState.Run);
}
else
{
SetAnimationState(PlayerState.Idle);
}
}
else
{
rb.MovePosition(rb.position + moveInput * (walkSpeed * Time.deltaTime));
// if there is meaningful movement, rotate player in the direction
// of the movement and set animation to walk
if (moveInput.magnitude >= 0.1f)
{
// change player rotation to face the movement direction.
Quaternion newRotation = Quaternion.LookRotation(moveInput, Vector3.up);
rb.rotation = Quaternion.Slerp(rb.rotation, newRotation, rotationSpeed);
// set the animation State to walking.
SetAnimationState(PlayerState.Walk);
}
else
{
SetAnimationState(PlayerState.Idle);
}
// Preform jump
if (jumpRequested && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
animator.SetTrigger(jump);
isGrounded = false;
jumpRequested = false;
}
}
}
private void SetAnimationState(PlayerState animState)
{
animator.SetInteger(State, (int)animState);
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}