当前位置:网站首页>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
边栏推荐
- 顺序栈 C语言 进栈 出栈 遍历
- QT novice entry level calculator addition, subtraction, multiplication, division, application
- data normalization
- 论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络
- String methods and instances
- Channel attention and spatial attention module
- Thymeleaf快速入门学习
- day6-jvm
- 使用Keras实现 基于注意力机制(Attention)的 LSTM 时间序列预测
- Draw contour cv2.findcontours function and parameter interpretation
猜你喜欢

碰壁记录(持续更新)

Hit the wall record (continuously updated)

STM32 DSP library MDK vc5\vc6 compilation error: 256, (const float64_t *) twiddlecoeff64_ 256, armBitRevIndexTableF64_ 256,

论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络

Day2 websocket+ sort

常见十大漏洞总结(原理、危害、防御)

Use QT to connect to MySQL and create table numbers, write data, and delete data

ue4换装系统 1.换装系统的基本原理

JUC并发编程基础(9)--线程池

使用Qt连接MySql并创建表号、写入数据、删除数据
随机推荐
Qt 使用纯代码画图异常
碰壁记录(持续更新)
Draw contour cv2.findcontours function and parameter interpretation
用指针访问一维数组
[MYCAT] MYCAT sub database and sub table
Find the ArrayList < double > with the most occurrences in ArrayList < ArrayList < double >
day4-jvm
PDF Text merge
Hit the wall record (continuously updated)
Search of two-dimensional array of "sword finger offer" C language version
餐饮数据统计分析---泰迪云课程大作业
Day2 websocket+ sort
Typora installation package in November 2021, the last free version of the installation package to download v13.6.1
systemctl + journalctl
HAL_ Delay() delay error about 1ms
QT char to qstring hexadecimal and char to hexadecimal integer
day6-jvm
Unity基础知识及一些基本API的使用
记一次高校学生账户密码的获取,从无到有
精确计算时间延迟VxWorks 时间戳 详解