JustPaste.it

Related Code

using System.Collections;
using UnityEngine;

public class PlayerStateMachine : StateMachine
{


    [field: SerializeField] public InputReader inputReader { get; private set; }
    [field: SerializeField] public Rigidbody rb { get; private set; }
    [SerializeField] public Transform cameraTarget { get; private set;}

    [field: SerializeField] public Animator animator { get; private set; }

    [SerializeField] public float moveSpeed = 5f;
    [SerializeField] public float cameraRotationDegrees = 45f;
    [SerializeField] public float cameraRotationDuration = .3f;
    private void Start()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody>();
        inputReader = GetComponent<InputReader>();

        SwitchState(new PlayerMovingState(this));
    }

    

    public IEnumerator RotateCameraTarget(Quaternion rotationOffset)
    {
        bool isRotating = true;
        Quaternion initialRotation = cameraTarget.rotation;
        Quaternion targetRotation = initialRotation * rotationOffset;
        float elapsed = 0;

        while(elapsed < cameraRotationDuration)
        {
            yield return new WaitForFixedUpdate();
            elapsed += Time.deltaTime;
            float t = Mathf.Clamp01(elapsed / cameraRotationDuration);
            cameraTarget.rotation = Quaternion.Slerp(initialRotation, targetRotation, t);
        }
        cameraTarget.rotation = targetRotation;
        isRotating = false;
    }

    

}


using System;
using UnityEngine;

public class PlayerMovingState : PlayerBaseState
{
    public PlayerMovingState(PlayerStateMachine stateMachine) : base(stateMachine)
    {
        this.stateMachine = stateMachine;
    }

    private float animationDampTime = 0.1f;

    public override void Enter() {
        stateMachine.animator.CrossFadeInFixedTime("LocomotionBlendTree", animationDampTime);
    }

    public override void Exit() { 
        
    }

    public override void Tick(float deltaTime)
    {

        CheckAttackStateAndChange();

        Vector3 movement = new Vector3(
            stateMachine.inputReader.movementValue.x,
            0f,
            stateMachine.inputReader.movementValue.y
        );

        // Move the rigidbody
        stateMachine.rb.MovePosition(
            stateMachine.rb.position + (movement * deltaTime * stateMachine.moveSpeed)
        );

        
        if (movement!=Vector3.zero)
        {
                
                
                stateMachine.animator.SetFloat("MoveX", movement.x, animationDampTime, deltaTime);
                stateMachine.animator.SetFloat("MoveZ", movement.z, animationDampTime, deltaTime);
            
        }
        else
        {
            stateMachine.animator.SetFloat("MoveX", 0, animationDampTime, deltaTime);
            stateMachine.animator.SetFloat("MoveZ", 0, animationDampTime, deltaTime);
        }
    }

    private void CheckAttackStateAndChange()
    {

        if (stateMachine.inputReader.isAttacking)
        {
            stateMachine.SwitchState(new PlayerAttackingState(stateMachine));
            return;
        }

    }
}



using System.Collections;
using UnityEngine;

public class PlayerAttackingState : PlayerBaseState
{
    private float dampTime = 0.1f;
    private string attackAnim = "Standing_1H_SpellCast_02";
    public PlayerAttackingState(PlayerStateMachine stateMachine) : base(stateMachine)
    {
        this.stateMachine = stateMachine;
    }

    public override void Enter()
    {

        //Debug.Log($"{stateMachine.animator.runtimeAnimatorController.layers}");
        stateMachine.animator.CrossFadeInFixedTime(attackAnim, dampTime,1);
    }

    public override void Tick(float deltaTime)
    {

        Vector3 movement = new Vector3(
            stateMachine.inputReader.movementValue.x,
            0f,
            stateMachine.inputReader.movementValue.y
        );


        if (movement != Vector3.zero)
        {


            stateMachine.animator.SetFloat("MoveX", movement.x, dampTime, deltaTime);
            stateMachine.animator.SetFloat("MoveZ", movement.z, dampTime, deltaTime);

        }
        else
        {
            stateMachine.animator.SetFloat("MoveX", 0, dampTime, deltaTime);
            stateMachine.animator.SetFloat("MoveZ", 0, dampTime, deltaTime);
        }
        // Move the rigidbody
        stateMachine.rb.MovePosition(
            stateMachine.rb.position + (movement * deltaTime * stateMachine.moveSpeed)
        );

        AnimatorStateInfo stateInfo = stateMachine.animator.GetCurrentAnimatorStateInfo(1);

        if (stateInfo.normalizedTime >= 1.0f)
        {
            stateMachine.SwitchState(new PlayerMovingState(stateMachine));
        }
        
            
    }

    public override void Exit()
    {
        stateMachine.animator.CrossFadeInFixedTime("BlankState", dampTime, 1);
        Debug.Log("leaving attack state");
        // Cleanup if necessary; avoid switching state here to prevent redundant calls.
    }
}