从零开始的类银河恶魔城游戏06
[自学]从零开始的类银河恶魔城游戏06
part 6:Dash State 2025/2/18
(Player.cs)
变量
public float dashSpeed = 10f;
public float dashDuration = 0.5f;
新增状态
public PlayerDashState dashState { get; private set; }
(PlayerState.cs)
定时器
//PlayerState.cs
public float stateTimer; //
public virtual void Update()
{
Debug.Log("I'm in " + animBoolName);
stateTimer -= Time.deltaTime; //定时器自减
xInput = Input.GetAxisRaw("Horizontal");
player.anim.SetFloat("yVelocity", rb.velocity.y);
}
//PlayerDashState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerDashState : PlayerState
{
public PlayerDashState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
stateTimer = player.dashDuration; // 定时器设置为冲刺持续时间dashduration
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
player.SetVelocity(player.dashSpeed * player.facingDir, rb.velocity.y); //设置冲刺速度
if(stateTimer < 0 )
{
stateMachine.ChangeState(player.idleState);
}
}
}
//PlayerGroundedState.cs
public override void Update()
{
base.Update();
if (Input.GetKeyDown(KeyCode.LeftShift)) //
{
stateMachine.ChangeState(player.dashState);
}
if (Input.GetKeyDown(KeyCode.Space) && player.IsGroundedDetected())
{
stateMachine.ChangeState(player.jumpState);
}
}
}
遇到的问题
animation
帧采样(Samples):右边三个点展开可以开启这个选项
此功能改动过的文件 的完整代码:
Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Callbacks;
using UnityEngine;
public class Player: MonoBehaviour
{
[Header("Move info")]
public float moveSpeed = 8f;
public float jumpForce = 15f;
public float dashSpeed = 10f;
public float dashDuration = 0.5f;
[Header("Collision info")]
[SerializeField]private Transform groundCheck;
[SerializeField]private float groundCheckDistance;
[SerializeField]private Transform wallCheck;
[SerializeField]private float wallCheckDistance;
[SerializeField]private LayerMask whatIsGround;
[SerializeField]private LayerMask whatIsWall;
public int facingDir {get; private set;} = 1;
private bool facingRight = true;
#region Components
public Animator anim { get; private set; }
public Rigidbody2D rb { get; private set; }
#endregion
#region States
public PlayerStateMachine stateMachine { get; private set; }
public PlayerIdleState idleState { get; private set; }
public PlayerMoveState moveState { get; private set; }
public PlayerJumpState jumpState { get; private set; }
public PlayerAirState airState { get;private set; }
public PlayerDashState dashState { get; private set; }
#endregion
private void Awake()
{
stateMachine = new PlayerStateMachine();
idleState = new PlayerIdleState(this,stateMachine,"Idle");
moveState = new PlayerMoveState(this,stateMachine,"Move");
jumpState = new PlayerJumpState(this,stateMachine,"Jump");
airState = new PlayerAirState(this,stateMachine, "Jump");
dashState = new PlayerDashState(this,stateMachine,"Dash");
}
public void Start()
{
anim = GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody2D>();
stateMachine.Initialize(idleState);// PlayerStateMachine.Initialize()
}
public void Update()
{
stateMachine.currentState.Update();// PlayerState.Update()
}
//设置速度
public void SetVelocity(float _xVelocity, float _yVelocity)
{
rb.velocity = new Vector2(_xVelocity,_yVelocity);
FlipController(_xVelocity);
}
//地面检测
//Physics2D.RayCast(起始点,方向,距离,检测层)
public bool IsGroundedDetected() => Physics2D.Raycast(groundCheck.position,Vector2.down,groundCheckDistance,whatIsGround);
//画碰撞检测射线
private void OnDrawGizmos()
{
//Gizmos.DrawLine(起始点,终点(x,y-距离))
//右上+,左下-
Gizmos.DrawLine(groundCheck.position,new Vector3(groundCheck.position.x,groundCheck.position.y-groundCheckDistance));
Gizmos.DrawLine(wallCheck.position,new Vector3(wallCheck.position.x+wallCheckDistance,wallCheck.position.y));
}
private void Flip()
{
facingRight = !facingRight;
facingDir *= -1;
transform.Rotate(0,180,0);//y轴180度翻转
}
private void FlipController(float _x)
{
if(_x > 0 && !facingRight)
{
Flip();
}
else if(_x < 0 && facingRight)
{
Flip();
}
}
}
PlayerState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerState
{
protected PlayerStateMachine stateMachine;
protected Player player;
protected Rigidbody2D rb;
protected float xInput;
private string animBoolName;
public float stateTimer;
public PlayerState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName)
{
this.player = _player;
this.stateMachine = _stateMachine;
this.animBoolName = _animBoolName;
}
public virtual void Enter()
{
player.anim.SetBool(animBoolName, true);
rb = player.rb;
}
public virtual void Update()
{
Debug.Log("I'm in " + animBoolName);
stateTimer -= Time.deltaTime; //定时器自减
xInput = Input.GetAxisRaw("Horizontal");
player.anim.SetFloat("yVelocity", rb.velocity.y);
}
public virtual void Exit()
{
player.anim.SetBool(animBoolName, false);
}
}
PlayerDashState.cs 如上
PlayerGroundedState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGroundedState : PlayerState
{
public PlayerGroundedState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if (Input.GetKeyDown(KeyCode.LeftShift))
{
stateMachine.ChangeState(player.dashState);
}
if (Input.GetKeyDown(KeyCode.Space) && player.IsGroundedDetected())
{
stateMachine.ChangeState(player.jumpState);
}
}
}

浙公网安备 33010602011771号