Unity 一个丝滑的3D下--XY轴2D平台跳跃--控制器模板(FSM)

本Demo基础来自于:

【阿严】Unity平台跳跃游戏 角色控制器 教程合集 | 状态机架构 | Platformer Controller Tutorial Collection_哔哩哔哩_bilibili
阿严的Github地址:https://github.com/AtCloudStudio/PlatformerControllerTutorial

我的实现:通过网盘分享的文件:3D下的2D控制器精髓.7z

链接: https://pan.baidu.com/s/1B-XHXM0a9VzVTrJbL3uctw?pwd=1234 提取码: 1234

本Demo的重点:

目录

1.新输入系统

[2. FSM的so文件化](#2. FSM的so文件化)

3.土狼时间

4.输入预处理(跳跃)


思维导图

状态机及玩家基本组件部分

玩家具体状态实现

视频介绍:

Unity 一个3D下的2D平台跳跃控制器

因为实现起来还是比较简单无痛的 所以具体请查看源码 我只是将代码放在这里 并不会做更多的解释

1.新输入系统

cs 复制代码
using System.Collections;
using Unity.VisualScripting;
using UnityEngine;

public class PlayerInput : MonoBehaviour
{
    private InputSystem_Actions inputActions;


    [SerializeField, InspectorLabel("跳跃缓冲自动无效时间")] private float jumpInputBufferTime = 0.5f;

    public Vector2 Axis => inputActions.Player.Move.ReadValue<Vector2>();
    public float MoveX_input => Axis.x;
    public bool isMove_input => MoveX_input != 0;
    public bool isJump_input => inputActions.Player.Jump.WasPerformedThisFrame();
    public bool StopJump_input => inputActions.Player.Jump.WasReleasedThisFrame();
    //土狼时间
    public bool hasJumpInputBuffer { get; set; }


    private void Awake()
    {
        inputActions = new InputSystem_Actions();
    }
    public void EnableInputSystem()
    {
        inputActions.Player.Enable();
        Cursor.lockState = CursorLockMode.Locked;

        // 注册 当玩家松开跳跃键时,就会触发canceled这个回调
        inputActions.Player.Jump.canceled += (de) =>
        {
            hasJumpInputBuffer = false;
        };
    }



    void OnGUI()
    {
        Rect rect = new Rect(200, 200, 200, 200);
        string message = "Has Jump Input Buffer: " + hasJumpInputBuffer;
        GUIStyle style = new GUIStyle();

        style.fontSize = 20;
        style.fontStyle = FontStyle.Bold;

        GUI.Label(rect, message, style);
    }

    public void ResetJumpBuffer()
    {
        StopCoroutine(DoRestJumpBuffer());
        StartCoroutine(DoRestJumpBuffer());
    }
    public IEnumerator DoRestJumpBuffer()
    {

        hasJumpInputBuffer = true;
        yield return new WaitForSeconds(jumpInputBufferTime);
        hasJumpInputBuffer = false;
    }

    public void DisableInputSystem()
    {
        inputActions.Player.Disable();
    }
}

2. FSM的so文件化

cs 复制代码
using UnityEngine;

public class PlayerState : ScriptableObject, IState
{
    protected Animator animator;
    protected PlayerInput playerInput;
    protected PlyaerStateMachine playerStateMachine;
    protected PlayerController playerController;

    #region 动画参数

    [SerializeField] private string animationClipName; //动画名称
    [SerializeField, Range(0, 1f)] private float crossFadeTime = 0.1f;
    private int stateHash;


    protected float startTime;
    protected float StateDurtion => Time.time - startTime;
    protected bool IsAnimationFinished => StateDurtion >= animator.GetCurrentAnimatorStateInfo(0).length;

    #endregion
    #region 继承参数
    protected float currentSpeed;
    #endregion

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="stateMachine">状态机对象</param>
    /// <param name="animator">动画组件</param>
    /// <param name="playerInput">玩家输入类对象</param>
    /// <param name="playerController">玩家控制类对象</param>
    public void Init(PlyaerStateMachine stateMachine, Animator animator, PlayerInput playerInput,
        PlayerController playerController)
    {
        this.animator = animator;
        this.playerStateMachine = stateMachine;
        this.playerInput = playerInput;
        this.playerController = playerController;
    }
    private void OnEnable()
    {
        stateHash = Animator.StringToHash(animationClipName);
    }
    public virtual void Enter()
    {
        animator.CrossFadeInFixedTime(animationClipName, crossFadeTime);
        startTime = Time.time;
    }

    public virtual void Exit() { }

    public virtual void FixedUpdate() { }

    public virtual void Update() { }
}

3.土狼时间

cs 复制代码
using UnityEngine;
[CreateAssetMenu(menuName = "Player/States/Player_CoyoteTime")]
public class Player_CoyoteTime : PlayerState
{
    public float runSpeed = 5f;
    public float coyoteTime = 0.2f;
    public override void Enter()
    {
        base.Enter();
        playerController.SetUesGravity(false);
    }

    public override void Update()
    {
        //土狼时间:让玩家在空中悬浮一小段时间 还是可以跳跃
        if (playerInput.isJump_input)
        {
            playerStateMachine.ChangeState(typeof(Player_JumpUp));
        }
        //只有在超过过土狼时间以后 或者不输入以后才会下落
        if (StateDurtion >= coyoteTime || !playerInput.isMove_input)
        {
            playerStateMachine.ChangeState(typeof(Player_JumpFall));
        }
    }

    public override void FixedUpdate()
    {
        //currentSpeed同步减速
        playerController.Move2Clip(runSpeed);
    }

    public override void Exit()
    {
        playerController.SetUesGravity(true);

    }
}

4.输入预处理(跳跃)

在PlayerInput里

cs 复制代码
    public void ResetJumpBuffer()
    {
        StopCoroutine(DoRestJumpBuffer());
        StartCoroutine(DoRestJumpBuffer());
    }
    public IEnumerator DoRestJumpBuffer()
    {

        hasJumpInputBuffer = true;
        yield return new WaitForSeconds(jumpInputBufferTime);
        hasJumpInputBuffer = false;
    }
相关推荐
jeffsonfu7 小时前
卷积不止于2D:掌握1D、2D、3D卷积的适用场景
3d
郝学胜-神的一滴8 小时前
Horse3D 游戏引擎研发笔记(七):Clydesdale——从流式日志到多输出订阅
c++·qt·unity·游戏引擎·图形渲染·unreal engine·opengl
江上闲人.1 天前
Wardogs战犬\战狗Beta测试启动报错UE崩溃卡顿?解决方法来了
游戏·游戏引擎·虚幻·战犬·战狗·wardogs
云飞云共享云桌面1 天前
自动化设备厂 10 人研发团队,如何共用一台服务器做 SolidWorks 建模?
运维·服务器·网络·3d·自动化·制造
小贺儿开发1 天前
Unity 程序员编曲花絮:汪峰《河流》细节修复
科技·学习·unity·程序员·音乐·demo·写代码
普密斯科技1 天前
在线图像测量仪HM-1120如何实现电机轴承从抽检到全检的跨越
计算机视觉·3d·集成测试·测量
598866753@qq.com1 天前
Unity UniText笔记
unity·unitext
杨丰玮4181 天前
从零手写Java飞机躲障碍游戏|Swing绘图、鼠标跟随、计时器碰撞检测实战(五)
java·python·游戏·游戏引擎·图形渲染·动画·贴图
鼎艺创新科技2 天前
不依赖 UE/Unity:我们如何从零搭建一套国产三维 GIS 渲染引擎
人工智能·算法·unity·游戏引擎·三维电子沙盘
2501_906565122 天前
3D曲面与普通平面的转换
3d