Unity类噩梦城 学习笔记10

Part10:Wall Jump State(P37)

1:效果和操作
效果:在墙体上时,能够完成向外(反方向)跳跃,
逻辑:当处于wallslide状态时,按下space则进入walljump状态,产生walljump的Enter方法的反方向跳动效果,之后不断进行walljump状态持续时间检测和地面检测,符合条件则转换为idle(1-4)
修复:1.修复空中dash碰到墙体可以转化为wallslide
2.使得碰撞墙体后不能继续dash
2:代码改变
//1.Player代码改变
public PlayerWallJumpState wallJump { get; private set; }
private void Awake()
    {
        jumpState = new PlayerJumpState(this, stateMachine, "Jump");
        airState = new PlayerAirState(this, stateMachine, "Jump");
        wallJump = new PlayerWallJumpState(this, stateMachine, "Jump");
  //三种状态同为Jump动画播放

    }
//2.新增PlayerWallJump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerWallJumpState : PlayerState
{
    public PlayerWallJumpState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {
     }
 
    public override void Enter()
    {
        base.Enter();
 
        stateTimer = 1f;//状态持续时间
        player.SetVelocity(5 * -player.facingDir, player.jumpForce);//跳是向反方向跳
    }
 
    public override void Exit()
    {
        base.Exit();
    }
 
    public override void Update()
    {
        base.Update();
        if(stateTimer<0)//小于状态持续时间则恢复为idle状态
        {
            stateMachine.ChangeState(player.idleState);
        }
        if (player.IsGroundDetected())//为防止接触地面还处于fall状态动画
        {
            stateMachine.ChangeState(player.idleState);
        }
    }
}
//3.PlayerWallSlideState代码改变
public override void Update()
    {
        base.Update();
        if(Input.GetKeyDown(KeyCode.Space))
        {
            stateMachine.ChangeState(player.wallJump);
            return; //防止进入其他状态
        }
}
//4.PlayerIdleState代码改变(使得转换idle无如何方向速度)
public override void Enter()
    {
        base.Enter();
        rb.velocity = new Vector2(0, 0);//使得落地(或其他状态切换为idle带有其他方向速度)
    }
//5.PlayerDashState代码改变(修复空中dash碰到墙体可以转化为wallslide)
 public override void Update()
    {
        base.Update();
        if(!player.IsGroundDetected()&&player.IsWallDetected())
       	//使得在空中dash也能在碰撞到墙体时进行slide                                                       
        {
            stateMachine.ChangeState(player.wallSlide);
        }
 }
//6.PlayerMoveState代码改变(使得碰撞墙体后不能继续move)
if (xInput==0|| player.IsWallDetected())
        {
            stateMachine.ChangeState(player.idleState)//使得碰撞到墙体则无法继续往前
        }

//Player代码改变(使得在与墙体碰撞则无法冲刺)
public void CheckForDashInput()
    {
        
        dashUsageTimer -= Time.deltaTime;
 
        if(IsWallDetected())//使得与墙体碰撞则无法dash
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.LeftShift) && dashUsageTimer < 0)
        {
 
            dashUsageTimer = dashCooldown;
            dashDir = Input.GetAxisRaw("Horizontal");
            if (dashDir == 0)
            {
                dashDir = facingDir;
            }
            stateMachine.ChangeState(dashState);
        }
        
    }
3:效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值