当前位置:网站首页>unity2D横版游戏跳跃实时响应
unity2D横版游戏跳跃实时响应
2022-07-24 05:22:00 【真的没事鸭】
如果我们把移动,跳跃等操作都放在Update函数里面我们会发现有时候我们按下跳跃键的时候角色并不会响应,对手感有很大的影响,所以我们结合Update和FixedUpdate实现准确的跳跃响应。

我们知道我们角色的移动,跳跃操作一般都要放在每一帧都要执行的函数里,每一帧执行的函数有很多,比如Update,LateUpdate,FixedUpdate等。LateUpdate我们暂且不提,简单说一下另外两个。
首先是Update,每一帧都会调用Update函数,这是帧更新的主要函数,但是我们要知道,对于不同的电脑每一秒执行的帧数是不一样的,比如一些比较老的电脑一秒执行30帧,好一点的电脑一秒执行40帧,所以为了要让不同的电脑也能实时的检测按键的响应,我们就要用到FixedUpdate函数。
FixedUpdate是一个固定的时间来执行的,如果没有修改的情况下是0.02秒执行一次FixedUpdate函数,也就是说一秒钟执行50次,我们也可以修改间隔时间,但是注意,这个值只是一个请求,机子可能达不到这个速度。因此,Update和FixedUpdate其实是完全独立的两个函数。当Update执行了一次时,FixedUpdate可能已经执行了两次、三次甚至一次都没有执行过。根据官方手册,所有和物理有关的都会放在FixedUpdate里面,比如Rigidbody。
我们介绍Update和FixedUpdate就是因为实现完美跳跃就是需要通过这两个函数实现的,我们把GetButtonDown放在Update里能够确保在不同的机器上我们按下跳跃键的时候,电脑可以给我们一个实时的那一帧的调用这个按键的反馈。而FixedUpdate会在固定时间执行,来检测按键按下的这个命令是否被执行。所以通过结合Update和FixedUpdate来实现这个操作。
using UnityEngine;
public class Test:MonoBehaviour{
bool action = false;
private Rigidbody2D rb;
public float jumpForce;
void Start(){
rb=GetComponent<Rigidbody2D>();
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
action = true;
}
}
void FixedUpdate(){
if(action){
rb.velocity=new Vector2(rb.velocity.x,jumpForce);
action = false;
}
}
}上面,我们用一个boolean值来控制开关。由于Update每一帧都会调用,因此可以保证所有的输入都可以被响应。
当然这只是一份伪代码,因为我们并没有考虑地面或者多段跳等情况,所以具体的代码还有具体情况分析
边栏推荐
- QT char to qstring hexadecimal and char to hexadecimal integer
- Conversion of world coordinate system, camera coordinate system and image coordinate system
- Answers and analysis of some after-school exercises in signals and systems (Wujing)
- Unity 3D帧率统计脚本
- HoloLens 2 中文开发文档 MRTK v2
- Jestson installs IBus input method
- 用指针访问二维数组
- day3-jvm+排序总结
- Sort ArrayList < ArrayList < double> >
- JUC并发编程基础(8)--读写锁
猜你喜欢

Day3 jvm+ sorting summary

Write the list to txt and directly remove the comma in the middle

MySQL foundation - constraints

餐饮数据统计分析---泰迪云课程大作业

Foundation of JUC concurrent programming (6) -- lock lock

Unicast, multicast, broadcast, tool development, introduction to QT UDP communication protocol development and source code of development tools
![[principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL](/img/51/7387c73148ee7bd1034bb6e77af7f0.png)
[principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL
![[activiti] gateway](/img/8c/c89ec6c4c5871a32218ddadfd8beba.png)
[activiti] gateway

day3-jvm+排序总结

AD1256
随机推荐
[raspberry pie 4B] VII. Summary of remote login methods for raspberry pie xshell, putty, vncserver, xrdp
Qt 使用纯代码画图异常
数组常用方法
Unity Shader :实现漫反射与高光反射
js星星打分效果
[deep learning] teach you to write "handwritten digit recognition neural network" hand in hand, without using any framework, pure numpy
AD1256
碰壁记录(持续更新)
PDF Text merge
Unity 3D帧率统计脚本
bat批处理脚本、同时运行多个文件、按照顺序执行的批处理命令及xshell脚本。
餐饮数据统计分析---泰迪云课程大作业
Unity Shader从内置渲染管线迁移到URP
Openwrt quick configuration Samba
unity最新版本的Text(TMP)UI文本怎么显示中文
Search of two-dimensional array of "sword finger offer" C language version
JVM系统学习
Bat batch script, running multiple files at the same time, batch commands executed in sequence, and xshell script.
HAL_ Delay() delay error about 1ms
Headlong platform operation