当前位置:网站首页>Unity2d horizontal game jump real-time response
Unity2d horizontal game jump real-time response
2022-07-24 06:12:00 【It's really all right, duck】
If we move , Jumping and other operations are put in Update In the function, we will find that sometimes when we press the jump key, the character will not respond , It has a great impact on the feel , So we combine Update and FixedUpdate Achieve accurate jump response .

We know the movement of our characters , The jump operation is usually placed in the function to be executed in each frame , There are many functions executed in each frame , such as Update,LateUpdate,FixedUpdate etc. .LateUpdate Let's not mention , Briefly, the other two .
First of all Update, Every frame calls Update function , This is the main function of frame update , But we need to know , For different computers, the number of frames executed per second is different , For example, some older computers execute in one second 30 frame , A better computer executes in one second 40 frame , So in order to make different computers can also detect the response of keys in real time , We're going to use FixedUpdate function .
FixedUpdate It is executed at a fixed time , If there is no modification, it is 0.02 Once per second FixedUpdate function , That is to say, one second execution 50 Time , We can also modify the interval , But notice , This value is just a request , The machine may not reach this speed . therefore ,Update and FixedUpdate It's actually two completely independent functions . When Update When executed once ,FixedUpdate It may have been executed twice 、 Three times, not even once . According to the official manual , Everything related to physics will be put in FixedUpdate Inside , such as Rigidbody.
Let's introduce Update and FixedUpdate It is because these two functions are needed to realize the perfect jump , We put GetButtonDown Put it in Update It can ensure that when we press the jump button on different machines , The computer can give us a real-time feedback of calling this button at that frame . and FixedUpdate It will be executed at a fixed time , To detect whether the command pressed by the key is executed . So by combining Update and FixedUpdate To do this .
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;
}
}
}above , We use one boolean Value to control the switch . because Update Every frame calls , Therefore, it can ensure that all inputs can be responded .
Of course, this is just a pseudocode , Because we didn't consider the situation of ground or multi jump , So the specific code and specific situation analysis
边栏推荐
- 使用Keras实现 基于注意力机制(Attention)的 LSTM 时间序列预测
- C language linked list (create, traverse, release, find, delete, insert a node, sort, reverse order)
- Unity 3D帧率统计脚本
- Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)
- day6-jvm
- bat批处理脚本、同时运行多个文件、按照顺序执行的批处理命令及xshell脚本。
- Jupyter notebook select CONDA environment
- Sequential stack C language stack entry and exit traversal
- Jestson installs IBus input method
- Use QT to connect to MySQL and create table numbers, write data, and delete data
猜你喜欢

HoloLens2开发:使用MRTK并且模拟眼动追踪

Day3 jvm+ sorting summary

单播、组播、广播、工具开发、QT Udp通讯协议开发简介及开发工具源码

MySql与Qt连接、将数据输出到QT的窗口tableWidget详细过程。

Foundation of JUC concurrent programming (8) -- read write lock

HoloLens 2开发:使用MRTK并在Unity中模拟手势输入

Unity(二) 更多API和物理引擎

Headlong platform operation

Thymeleaf quick start learning

String methods and instances
随机推荐
Vsual studio 2013 environment UDP multicast
Dameng database_ LENGTH_ IN_ Influence of char and charset
Common methods of array
CRC-16 MODBUS code
MySQL download and installation environment settings
Installation of tensorflow and pytorch frames and CUDA pit records
[FatFs] migrate FatFs manually and transfer SRAM virtual USB flash disk
UE4:浅谈什么是GamePlay框架
Unity Shader从内置渲染管线迁移到URP
【数据库系统原理】第五章 代数和逻辑查询语言:包、扩展操作符、关系逻辑、关系代数与Datalog
STM32 DSP library MDK vc5\vc6 compilation error: 256, (const float64_t *) twiddlecoeff64_ 256, armBitRevIndexTableF64_ 256,
Unity(三)三维数学和坐标系统
【数据库系统原理】第四章 高级数据库模型:统一建模语言UML、对象定义语言ODL
Dameng database_ Common user management commands
Qt新手入门级 计算器加、减、乘、除、应用
UDP通讯应用于多种环境的Demo
Statistical analysis of catering data --- Teddy cloud course homework
Headlong platform operation
[raspberry pie 4B] VII. Summary of remote login methods for raspberry pie xshell, putty, vncserver, xrdp
Xshell remote access tool