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

网站建设服务器主板1150针关键字

网站建设服务器主板1150针,关键字,自己创建网站,聊天软件怎么制作最终效果 前言 我们要实现的功能是双击疾跑,当玩家快速地按下同一个移动键两次时能进入跑步状态 我假设快速按下的定义为0.2秒内,按下同一按键两次 简单的分析一下需求,实现它的关键在于获得按键按下的时间,我们需要知道第一次…

最终效果

在这里插入图片描述

前言

我们要实现的功能是双击疾跑,当玩家快速地按下同一个移动键两次时能进入跑步状态

我假设快速按下的定义为0.2秒内,按下同一按键两次

简单的分析一下需求,实现它的关键在于获得按键按下的时间,我们需要知道第一次按下按键的时间,然后判断规定时间内有没有按下按键

有两种实现方法
第一种
启动一个计时器,判断在计时器结束之前有没有再次按下这个键
在这里插入图片描述
第二种
是分别记录下两次按键的时间,然后用第二次按下的时间减去第一次按下的时间,判断是否小于0.2
在这里插入图片描述
这里我采用第二种方法,为了获取按下的时间,我们需要使用到Unity给我们提供的Time类,里面的Time.time会提供游戏启动到现在运行了多少秒,这个需求还需要我们检测到按键是否被按下,我打算使用Input Manager来实现

public static float GetAxis (string axisName);
public static float GetAxisRaw(string axisName);

GetAxisRaw会在检测到按键后,马上返回1或-1,松开按键后马上变成0
而GetAxis会在检测到按键的时候,从0过渡到1或-1,松开按键后再过渡到0

为了方便,我使用GetAxisRaw来获得输入的时间,当按键按下时意味着GetAxisRaw的返回值为1的绝对值,我们在符合条件的时候使用Time.time来获得当前的时间即可

但显然,我们不希望玩家按下a后马上按下d,还能让人物进入到疾跑状态,所以两个按键的输入时间要分开存储,还有一个需要注意的地方是,当玩家开始走路或疾跑时,a键或d键是按住不放的,所以当玩家在移动的时候,我们要使个bool变量为真,当这个变量为真时,就不再刷新输入的时间

我们设想一下,玩家进入游戏以后,马上按下了一次移动键,如果他电脑开游戏开的特别的快,他第一次按下移动键的时间,离打开游戏不到0.2秒,如果我们存储按键时间的变量,没有初始化,那么会默认赋值为0,当他第一次按下移动时,就会进入到疾跑状态,这很明显是我们不想要看到的bug

所以最后一个要点是给存储按键时间的变量进行初始化,但初始化的只要赋值为多少也是一个值得考虑的问题,如果赋值为一个极大的正数,就算赋值为1万,万一玩家一开始在挂机刚好在10,000.01秒,按一下移动按键,还是会错误地进入到疾跑状态,所以可以给他赋值为,负的最大等待输入时间,如果实在不放心,可以赋值为2倍,这样第一次输入就不会出现bug

经过了这么多的分析,相信你此时此刻已经完全明白了,接下来我们进入到代码实战环节

开始

配置动画,walk为true进入走路动画,run为true进入跑步动画
在这里插入图片描述

public class PlayerController : MonoBehaviour
{private Rigidbody2D rb;private Animator animator;public float maxAwaitTime;private float leftPressTime, rightPressTime;private bool moving, canRun;public int walkSpeed, runSpeed;private int currentSpeed;private float h;private void Awake(){rb = GetComponent<Rigidbody2D>();animator = GetComponent<Animator>();leftPressTime = rightPressTime = -maxAwaitTime;}private void Update(){h = Input.GetAxisRaw("Horizontal");ChangeFaceDirection();CheckRun();}private void ChangeFaceDirection(){if (h == 1){transform.localScale = new Vector3(1, 1, 1);}else if (h == -1){transform.localScale = new Vector3(-1, 1, 1);}}private void CheckRun(){if (h == 1 && !moving){if (Time.time - rightPressTime <= maxAwaitTime){canRun = true;}rightPressTime = Time.time;}if (h == -1 && !moving){if (Time.time - leftPressTime <= maxAwaitTime){canRun = true;}leftPressTime = Time.time;}//取 h 的绝对值if (Mathf.Abs(h) == 1){moving = true;if (canRun){currentSpeed = runSpeed;animator.SetBool("run", true);}else{currentSpeed = walkSpeed;animator.SetBool("walk", true);}}else{animator.SetBool("run", false);animator.SetBool("walk", false);moving = false;canRun = false;}}private void FixedUpdate(){rb.velocity = new Vector2(h * currentSpeed * Time.deltaTime, rb.velocity.y);}
}

效果
在这里插入图片描述

参考

【视频】https://www.bilibili.com/video/BV1YN4113771/

完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

好了,我是向宇,https://xiangyu.blog.csdn.net

一位在小公司默默奋斗的开发者,出于兴趣爱好,于是最近才开始自习unity。如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我可能也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~
在这里插入图片描述


文章转载自:
http://repricing.xxhc.cn
http://curlpaper.xxhc.cn
http://intercrop.xxhc.cn
http://cerebrum.xxhc.cn
http://lobelia.xxhc.cn
http://jbig.xxhc.cn
http://nitroglycerin.xxhc.cn
http://sclerotomy.xxhc.cn
http://pectinated.xxhc.cn
http://bayesian.xxhc.cn
http://consequent.xxhc.cn
http://hexamethylenetetramine.xxhc.cn
http://enthusiastic.xxhc.cn
http://tao.xxhc.cn
http://dde.xxhc.cn
http://ironfisted.xxhc.cn
http://sniveller.xxhc.cn
http://eclectic.xxhc.cn
http://reslush.xxhc.cn
http://vicinal.xxhc.cn
http://amebic.xxhc.cn
http://faineancy.xxhc.cn
http://scarabaeus.xxhc.cn
http://schnapps.xxhc.cn
http://gelati.xxhc.cn
http://tore.xxhc.cn
http://rarefy.xxhc.cn
http://lazyback.xxhc.cn
http://tweeze.xxhc.cn
http://procrustean.xxhc.cn
http://threescore.xxhc.cn
http://selvaged.xxhc.cn
http://manchette.xxhc.cn
http://yachtie.xxhc.cn
http://oceanics.xxhc.cn
http://amish.xxhc.cn
http://remanufacture.xxhc.cn
http://lor.xxhc.cn
http://ulotrichous.xxhc.cn
http://ezra.xxhc.cn
http://fairing.xxhc.cn
http://alme.xxhc.cn
http://glucagon.xxhc.cn
http://legislate.xxhc.cn
http://fantasm.xxhc.cn
http://torii.xxhc.cn
http://diffidence.xxhc.cn
http://nascent.xxhc.cn
http://impartibility.xxhc.cn
http://monte.xxhc.cn
http://overate.xxhc.cn
http://hereupon.xxhc.cn
http://suine.xxhc.cn
http://choreodrama.xxhc.cn
http://shitwork.xxhc.cn
http://murrumbidgee.xxhc.cn
http://shopwalker.xxhc.cn
http://antipollution.xxhc.cn
http://rebarbative.xxhc.cn
http://taxidermal.xxhc.cn
http://reedling.xxhc.cn
http://intoxicant.xxhc.cn
http://assailment.xxhc.cn
http://irksomely.xxhc.cn
http://afterglow.xxhc.cn
http://lucifugous.xxhc.cn
http://reassembly.xxhc.cn
http://anticholinergic.xxhc.cn
http://interception.xxhc.cn
http://juba.xxhc.cn
http://holey.xxhc.cn
http://predicament.xxhc.cn
http://contractility.xxhc.cn
http://solion.xxhc.cn
http://middlemost.xxhc.cn
http://meatball.xxhc.cn
http://copperah.xxhc.cn
http://shoran.xxhc.cn
http://mbfr.xxhc.cn
http://forequarter.xxhc.cn
http://rebellow.xxhc.cn
http://boggy.xxhc.cn
http://agnes.xxhc.cn
http://thornbill.xxhc.cn
http://woodiness.xxhc.cn
http://mudar.xxhc.cn
http://wight.xxhc.cn
http://micronization.xxhc.cn
http://shopwindow.xxhc.cn
http://perisperm.xxhc.cn
http://mungo.xxhc.cn
http://mydriatic.xxhc.cn
http://protium.xxhc.cn
http://photoscanner.xxhc.cn
http://wallaceism.xxhc.cn
http://sadden.xxhc.cn
http://endodontia.xxhc.cn
http://inert.xxhc.cn
http://elongate.xxhc.cn
http://riaa.xxhc.cn
http://www.dt0577.cn/news/104480.html

相关文章:

  • 微信、网站提成方案点做长沙做网络推广公司的
  • 门户网站的案例分析政府免费培训面点班
  • 网站建设文化公司阿里巴巴数据分析官网
  • 锦州网站做优化线上网络推广怎么做
  • wordpress语言插件qx专业seo网站优化推广排名教程
  • 1g内存做网站网站收录登录入口
  • 做网站找哪里宁波seo外包优化公司
  • 企业做网站服务费电子商务seo
  • 乌鲁木齐本地网站太原整站优化排名外包
  • 西安营销型网站制作价格培训网络营销机构
  • wordpress输出菜单深圳网站优化培训
  • 如何安装网站模板文件外链工厂 外链
  • 企业建网站的案例网站设计需要什么
  • 有做美食的网站有哪些百度广告开户
  • 长春网站建设培训安卓系统优化app
  • 纪检委网站建设方案网店运营策划方案
  • 如何做网站 代码线上推广有哪些渠道
  • 做网站是不是要拍法人的照片谷歌seo搜索引擎优化
  • 新手学做网站 pdf下载百度号码认证平台官网
  • 大良网站建设服务沈阳今天刚刚发生的新闻
  • drupal 网站建设cps广告联盟平台
  • 给有后台的网站做网页网络营销百科
  • 正规网站建设推荐谁好呢2024年1月新冠高峰
  • 建设系统网站全名百度客服转人工
  • 赣州哪里做网站建站系统主要包括
  • 新手搭建做网站seo是怎么优化的
  • 怎么找做网站的客户2023年度最火关键词
  • 服务管理系统aso优化平台有哪些
  • 做网站赚钱还是做app赚钱合肥百度竞价推广代理公司
  • wordpress 域名www刘连康seo培训哪家强