当前位置:网站首页>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
边栏推荐
- 单播、组播、广播、工具开发、QT Udp通讯协议开发简介及开发工具源码
- JDBC elementary learning ------ (learning from Shang Silicon Valley)
- 用指针访问二维数组
- JDBC初级学习 ------(师承尚硅谷)
- MySql与Qt连接、将数据输出到QT的窗口tableWidget详细过程。
- HAL_ Delay() delay error about 1ms
- Foundation of JUC concurrent programming (8) -- read write lock
- Foundation of JUC concurrent programming (1) -- related basic concepts
- Foundation of JUC concurrent programming (7) -- multithread lock
- JUC concurrent programming foundation (9) -- thread pool
猜你喜欢
随机推荐
C language linked list (create, traverse, release, find, delete, insert a node, sort, reverse order)
STM32 standard peripheral Library (Standard Library) official website download method, with 2021 latest standard firmware library download link
Solve modularnotfounderror: no module named "cv2.aruco“
How to solve the problem of large distribution gap between training set and test set
JUC concurrent programming foundation (9) -- thread pool
Read the qualified line of CSV file and write it to another CSV
QT char to qstring hexadecimal and char to hexadecimal integer
Yolov5 learning summary (continuously updated)
Openpose Unity 插件部署教程
day6-jvm
Find the ArrayList < double > with the most occurrences in ArrayList < ArrayList < double >
Dameng database_ Common commands
Dameng database_ LENGTH_ IN_ Influence of char and charset
单播、组播、广播、工具开发、QT Udp通讯协议开发简介及开发工具源码
unity最新版本的Text(TMP)UI文本怎么显示中文
Better CV link collection (dynamic update)
day1-jvm+leetcode
day3-jvm+排序总结
Find the number with the most occurrences in the array
JUC并发编程基础(1)--相关基础概念









