Link: Assets - Google Drive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_airState : PlayerState
{
// Start is called before the first frame update
public Player_airState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
Debug.Log(“air”);
}
public override void Update()
{
base.Update();
if (player.IsGroundDetected()){//return true even it is in air
stateMachine.changeState(player.idleState);
}
}
public override void Exit()
{
base.Exit();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header(“Move Info”)]
public float moveSpeed = 4f;
public float jumpHeight = 12f;
public bool Ground;
[Header(“Collision Info”)]
[SerializeField] public LayerMask whatIsGround;
[SerializeField] public LayerMask whatIsWall;
[SerializeField] public Transform groundCheck;
[SerializeField] public float groundCheckRadius;
[SerializeField] public Transform wallCheck;
[SerializeField] public float wallCheckRadius;
#region Components
public Animator anim{ get; private set; }
public Rigidbody2D rb{ get; private set; }
#endregion
#region States
public PlayerStateMachine stateMachine{ get; private set; }
public Player_idleState idleState{ get; private set; }
public Player_moveState moveState{ get; private set; }
public Player_groundState groundState{ get; private set; }
public Player_jumpState jumpState{ get; private set; }
public Player_airState airState{ get; private set; }
#endregion
public void Awake()
{
stateMachine = new PlayerStateMachine();
idleState = new Player_idleState(this,stateMachine,“Idle”);
moveState = new Player_moveState(this, stateMachine, “Move”);
jumpState = new Player_jumpState(this, stateMachine, “Jump”);
airState = new Player_airState(this, stateMachine, “Jump”);
}
// Start is called before the first frame update
void Start()
{
Debug.Log(“Player Started”);
anim = GetComponentInChildren();
rb = GetComponent();
stateMachine.initialize(idleState);
}
public bool IsGroundDetected() => Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckRadius, whatIsGround);
public bool IsWallDetected() => Physics2D.Raycast(wallCheck.position, Vector2.right, wallCheckRadius, whatIsWall);
// Update is called once per frame
void Update()
{
stateMachine.currentState.Update();
}
public void SetVelocity(float _xvelocity, float _yvelocity)
{
rb.velocity = new Vector2(_xvelocity, _yvelocity);
}
private void OnDrawGizmos()
{
Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x+wallCheckRadius, wallCheck.position.y));
Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y-groundCheckRadius));
}
}