当前位置: 首页 > wzjs >正文

烟台专业做网站的公司今日最近的新闻大事10条

烟台专业做网站的公司,今日最近的新闻大事10条,锦州网站建设公司,网页个人简历模板本文章给大家分享一下如何实现基本的移动和进阶的跳跃(跳跃缓冲、可变跳跃、土狼时间)以及相对应的重力代码,大家可以根据自己的需要自行修改 实现效果 场景搭建 因为Godot不像Unity,一个节点只能绑定一个脚本,所以我…

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

实现效果


场景搭建

因为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 _jumpDeadZoneTime;    // 跳跃死区时间(短时间内跳跃输入无效)[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,左为 -1if (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 _jumpDeadZoneTime;     // 按键死区(防止短按立刻进入长跳)private float _coyoteTime;           // 土狼时间(离地后仍可跳的时间)// 跳跃状态计时器private float _jumpBufferTimer;      // 当前跳跃缓冲计时private float _jumpCumulativeTimer;  // 当前可变跳跃剩余时间private float _jumpHoldZoneTimer;    // 按住跳跃键的持续时间public float _coyoteTimer;           // 当前土狼时间计时器private float _jumpCumulativeForce;  // 当前可变跳跃施加的力度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;_jumpDeadZoneTime = player._jumpDeadZoneTime;_coyoteTime = player._coyoteTime;}public override void _PhysicsProcess(double delta){Jump((float)delta);PlayerGravity((float)delta);}private void Jump(float delta){// 跳跃缓冲:按下跳跃键时记录缓冲时间if (Input.IsActionJustPressed("Jump")){_jumpBufferTimer = _jumpBufferTime;}else{_jumpBufferTimer -= delta;}// 死区检测:跳跃中按住跳跃键时间if (Input.IsActionPressed("Jump") && _isJumping){_jumpHoldZoneTimer += delta;}else{_jumpHoldZoneTimer = 0f;}// 可变跳跃高度控制(长按跳得更高)if (Input.IsActionPressed("Jump") && _isJumping && _jumpCumulativeTimer > 0f && _jumpHoldZoneTimer > _jumpDeadZoneTime){player.Velocity = new Vector2(player.Velocity.X, player.Velocity.Y - _jumpCumulativeForce * delta);_jumpCumulativeTimer -= delta;}// 提前松开跳跃键:终止可变跳跃else if (Input.IsActionJustReleased("Jump")){_jumpCumulativeForce = 0f;_isJumping = false;}// 满足缓冲与土狼时间,可以跳跃if (_jumpBufferTimer > 0f && _coyoteTimer > 0f){player.Velocity = new Vector2(player.Velocity.X, player.Velocity.Y - _jumpForce);_jumpBufferTimer = 0f;_jumpCumulativeTimer = _jumpCumulativeTime;_jumpCumulativeForce = _jumpCumulative;_isJumping = true;}// 玩家在地面或攀爬中,重置跳跃状态if ((player.IsOnFloor() || player._isClimbing) && MathF.Abs(player.Velocity.Y) < 0.1f){_jumpCumulativeTimer = 0f;_downTempSpeed = 0f;_coyoteTimer = _coyoteTime;_isJumping = false;}else{_coyoteTimer -= delta;}// 限制最大跳跃速度(防止过快)if (player.Velocity.Y < -_jumpForce){player.Velocity = new Vector2(player.Velocity.X, -_jumpForce);}// 顶头检测,取消跳跃力if (player.RayHead.IsColliding()){_jumpCumulativeTimer = 0f;_jumpCumulativeForce = 0f;player.Velocity = new Vector2(player.Velocity.X, _gravity / 4);}}private void PlayerGravity(float delta){// 空中才加重力if (player.IsOnFloor()) return;player.Velocity = new Vector2(player.Velocity.X, player.Velocity.Y + _gravity * delta);}
}

总结

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

http://www.dtcms.com/wzjs/403026.html

相关文章:

  • 政府机关单位网站建设哈市今日头条最新
  • 如何建设 营销型 网站网页设计素材网站
  • 动态网站做什么好看网站推广的基本方法为
  • 如何建立网站教程产品如何做市场推广
  • 网站开发私人培训想开广告公司怎么起步
  • 做网站需要公司北京网站推广营销服务电话
  • 惠州哪个房地产网站做的比较好百度搜索榜
  • 要修改wordpress目录下的文件权限首页关键词排名优化
  • 兖州网站建设百度视频seo
  • 免费网站建设公司电脑零基础培训学校
  • 两学一做11月答题网站杭州关键词推广优化方案
  • 商城网站设计需要哪些技术google play下载
  • 网站案例模版电脑培训中心
  • 全国信息企业公示网官网查询百度起诉seo公司
  • 四川建设网站外贸独立站建站
  • 京津冀协同发展调研报告北京网站seo技术厂家
  • 内蒙古建设厅门户网站免费seo推广软件
  • 想学app开发哪里有培训班商丘优化公司
  • 大学科技园东区 做网站百度网盘app免费下载安装老版本
  • 房地产网站建设公司推荐新手小白怎么做跨境电商
  • 做房地产资质是什么网站查网站关键词快速排名服务
  • 福州中小企业网站制作百度一下官方网页版
  • 代理登录网站百度提交网址入口
  • 怎么样自己做网站今天的新闻主要内容
  • 做亚马逊联盟一定要有网站吗成人用品哪里进货好
  • 仿win8网站模板网站优化排名软件网
  • 网站刚建好怎么做能让百度收录品牌营销做得好的品牌有哪些
  • 好的h5网站模板seo关键词是怎么优化的
  • c2c网站建设自己如何制作一个网页
  • 支付宝可以给第三方网站做担保么推广怎么做才可以赚钱