当前位置:网站首页>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
边栏推荐
- Unity(三)三维数学和坐标系统
- Unity Shader从内置渲染管线迁移到URP
- Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)
- Foundation of JUC concurrent programming (8) -- read write lock
- 【数据库系统原理】第四章 高级数据库模型:统一建模语言UML、对象定义语言ODL
- Thymeleaf quick start learning
- Positional argument after keyword argument
- ue4 物品随机生成
- MySql下载,及安装环境设置
- 使用Keras实现CNN+BiLSTM+Attention的多维(多变量)时间序列预测
猜你喜欢

Calculation steps of principal component analysis

Hit the wall record (continuously updated)
![Oserror: [winerror 127] the specified program cannot be found. Error loading “caffe2_detectron_ops.dll“ or one of its dependencies](/img/1d/4c9924c20f697011f0e9cda6616c12.png)
Oserror: [winerror 127] the specified program cannot be found. Error loading “caffe2_detectron_ops.dll“ or one of its dependencies

day3-jvm+排序总结

Lua基础

10大漏洞评估和渗透测试工具

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

Day-7 JVM end

unity2D游戏之让人物动起来-下
![[FatFs] migrate FatFs manually and transfer SRAM virtual USB flash disk](/img/fb/5f3d17f1f3d6e4979ece5126e2925e.png)
[FatFs] migrate FatFs manually and transfer SRAM virtual USB flash disk
随机推荐
Yolov5 learning summary (continuously updated)
力扣:1-两数之和
JDBC初级学习 ------(师承尚硅谷)
STM32 standard peripheral Library (Standard Library) official website download method, with 2021 latest standard firmware library download link
餐饮数据统计分析---泰迪云课程大作业
The problem that the user name and password are automatically filled in when Google / Firefox manages the background new account
Oserror: [winerror 127] the specified program cannot be found. Error loading “caffe2_detectron_ops.dll“ or one of its dependencies
C language linked list (create, traverse, release, find, delete, insert a node, sort, reverse order)
Unity基础知识及一些基本API的使用
头歌 平台作业
Search of two-dimensional array of "sword finger offer" C language version
vsual studio 2013环境 Udp组播
Bat batch script, running multiple files at the same time, batch commands executed in sequence, and xshell script.
JUC并发编程基础(8)--读写锁
Installation of tensorflow and pytorch frames and CUDA pit records
unity2D横版游戏跳跃实时响应
简单却好用:使用Keras 2实现基于LSTM的多维时间序列预测
JDBC advanced -- learning from Shang Silicon Valley (DAO)
JVM系统学习
[principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL