[Godot] C#2D平台游戏基础移动和进阶跳跃代码

本文章给大家分享一下如何实现基本的移动和进阶的跳跃(跳跃缓冲、可变跳跃、土狼时间)以及相对应的重力代码,大家可以根据自己的需要自行修改

实现效果


场景搭建

因为Godot不像Unity,一个节点只能绑定一个脚本,所以我们可以创建一个节点,用来存放绑定各种脚本,这样能大大规范化我们的代码,毕竟一个脚本写几千行代码相信大家也不想经历(本人曾经深受其害)

有一点,玩家的碰撞体最好用胶囊或者其他的边角圆润的形状,否则我们直接绑的一个方块,会导致跳跃到平台边缘根本上不去

其中的RayCast2D节点是我用来判断玩家顶头的,大家可以根据需要调整,下面我给大家分享一下代码


代码

Player

using Godot;
using System;

public partial class Player : CharacterBody2D
{
    [ExportCategory("Item")]
    
    [Export] public float _moveSpeed;           // 玩家移动速度
    [Export] public float _jumpForce;           // 跳跃初始力度(用于起跳)
    [Export] public float _jumpCumulative;      // 可变跳跃额外加速度(持续跳跃时叠加)
    [Export] public float _downSpeed;           // 快速下落的速度(按下下落键时使用)
    [Export] public float _gravity;             // 重力加速度(用于下落)

    [ExportGroup("Time")]

    [Export] public float _jumpBufferTime;      // 跳跃缓冲时间(提前按跳跃键后多长时间内仍有效)
    [Export] public float _jumpCumulativeTime;  // 跳跃蓄力时间(可变跳跃持续多久)
    [Export] public float _coyoteTime;          // 土狼时间(离地后多久内仍可跳跃)

    [ExportGroup("Node")]

    [Export] public RayCast2D RayHead;          // 检测头顶碰撞的射线(用于防止跳跃时顶头)

    public override void _PhysicsProcess(double delta)
    {
        MoveAndSlide(); // 移动函数(实际处理碰撞和滑动)
    }
}

PlayerMove

using Godot;
using System;

public partial class PlayerMove : Node2D
{
    private Player player;         // 引用 Player 脚本,用于访问属性和控制玩家

    public override void _Ready()
    {
        // 获取 Player 脚本(父节点 -> 父节点 -> Player)
        player = GetParent().GetParent<Player>();
    }

    public override void _PhysicsProcess(double delta)
    {
        Move(); // 每帧执行移动逻辑
    }

    private void Move()
    {
        float directionX = 0f; // 水平方向,右为 +1,左为 -1

        if (Input.IsActionPressed("Right"))
        {
            directionX += 1f;
        }
        if (Input.IsActionPressed("Left"))
        {
            directionX -= 1f;
        }

        // 设置玩家水平速度,垂直方向保持不变
        player.Velocity = new Vector2(directionX * player._moveSpeed, player.Velocity.Y);
    }
}

PlayerJump

using Godot;
using System;

public partial class PlayerJump : Node2D
{
    private Player player;

    // 跳跃数值参数
    private float _jumpForce;            // 跳跃初始力度
    private float _jumpCumulative;       // 可变跳跃额外力度
    private float _downSpeed;            // 下坠加速度
    private float _gravity;              // 重力值

    // 时间控制参数
    private float _jumpBufferTime;       // 跳跃缓冲持续时间
    private float _jumpCumulativeTime;   // 可变跳跃持续时间
    private float _coyoteTime;           // 土狼时间(离地后仍可跳的时间)

    // 跳跃状态计时器
    private float _jumpBufferTimer;      // 当前跳跃缓冲计时
    private float _jumpCumulativeTimer;  // 当前可变跳跃剩余时间
    public float _coyoteTimer;           // 当前土狼时间计时器
    private float _downTempSpeed;        // 临时下坠速度(用于中断跳跃)

    private bool _isJumping;             // 是否处于跳跃状态(长跳过程)

    public override void _Ready()
    {
        // 获取参数引用
        player = GetParent().GetParent<Player>();
        _jumpForce = player._jumpForce;
        _gravity = player._gravity;
        _jumpCumulative = player._jumpCumulative;
        _downSpeed = player._downSpeed;
        _jumpBufferTime = player._jumpBufferTime;
        _jumpCumulativeTime = player._jumpCumulativeTime;
        _coyoteTime = player._coyoteTime;
    }

    public override void _PhysicsProcess(double delta)
    {
        Jump((float)delta);
    }

    private void Jump(float delta)
    {
        if (Input.IsActionJustPressed("Jump")) //跳跃缓冲
        {
            _jumpBufferTimer = _jumpBufferTime;
        }
        else
        {
            _jumpBufferTimer -= delta;
        }

        if ((player.IsOnFloor() || _coyoteTimer > 0f) && _jumpBufferTimer > 0f) //设置跳跃
        {
            _isJumping = true;
            _jumpCumulativeTimer = _jumpCumulativeTime;
        }

        if (Input.IsActionPressed("Jump") && _isJumping && _jumpCumulativeTimer > 0f)    //可变跳跃高度
        {
            player.Velocity = new Vector2(player.Velocity.X, -_jumpForce - _jumpCumulative * delta);
            _jumpCumulativeTimer -= delta;
            _jumpBufferTimer = 0f;
        }
        else if (Input.IsActionJustReleased("Jump"))
        {
            _isJumping = false;
        }

        if (player.IsOnFloor() && MathF.Abs(player.Velocity.Y) < 0.1f)    //土狼时间
        {
            _jumpCumulativeTimer = 0f;
            _downTempSpeed = 0f;
            _coyoteTimer = _coyoteTime;
            _isJumping = false;
        }
        else
        {
            _coyoteTimer -= delta;
        }

        if (player.RayHead.IsColliding())  //判断顶头
        {
            _jumpCumulativeTimer = 0f;
            player.Velocity = new Vector2(player.Velocity.X, _gravity / 4);
        }

        if (!player.IsOnFloor())
        {
            PlayerGravity(delta);
        }
    }

    private void PlayerGravity(float delta)
    {
        player.Velocity = new Vector2(player.Velocity.X, player.Velocity.Y + _gravity * delta);
    }
}

总结

给大家的代码还算比较完整,我实践测试感觉手感还是不对,大家根据自己的需要进行修改优化吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭逍遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值