当前位置:网站首页>Unity about some possible reasons and solutions for using addforce of rigidbody but it doesn't work
Unity about some possible reasons and solutions for using addforce of rigidbody but it doesn't work
2022-07-18 21:18:00 【viko_ cake】
List of articles
The first half of the article will summarize the possible causes and solutions found in the test process , It should be noted that the article takes the role jump by pressing the space as the test point , The latter part is to show the official information consulted during the test API Explanation and analysis .
V I K O _ C A K E
Possible causes + resolvent
- When the input method you use is :Input.GetKeyDown(KeyCode.Space), You can try in Addforce Add parameters after the function :ForceMode.VelocityChange, as follows :
void jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * force,ForceMode.VelocityChange); //1
}
}
- With the above differences , When the input method you use is :Input.GetButton(“Jump”) The following code does not need to add parameters , It should be noted that Input.GetButtonDown(“Jump”) It can't be
if (Input.GetButton("Jump"))
{
rb.AddForce(Vector3.up * force); //1
}
- If the code is rewritten as effective as the above code, it still has no effect , Try checking Rigidbody Whether the component has dynamic rigid bodies turned on ( Use AddForce This option cannot be turned on )

- Code logic problems , When you check all parts but still have no effect, you can try to test whether there is other logic in the code . For example, the author adds a judgment to judge whether the character is on the ground , This leads to logical problems :
private void Update()
{
// Jump monitor
if (Input.GetButton("Jump") && canJump )
{
ani.SetTrigger("isjump");
rb.AddForce(transform.up * jumpForce);
canJump = false;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
canJump = true;
}
}
When you simply read the above code, you will find Update Medium Addforce Method will only execute One frame , It looks like Addforce No effect , This is obviously not in line with our expectations , How to modify , You guys can modify it as follows :
private void Update()
{
// Jump monitor
if (Input.GetButton("Jump") && canJump )
{
ani.SetTrigger("isjump");
rb.AddForce(transform.up * jumpForce);
//canJump = false; This sentence cannot be used here
}
if (Input.GetButtonUp("Jump"))
{
canJump = false;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
canJump = true;
}
}
It involves API And analysis
- For the first and second reasons , The author is looking up relevant materials GetButton and GetxxxDown The difference between the two is that they return true Duration ,GetxxxDown It will only return at the pressed frame time true, and GetButton Will return during pressing true.


And why right AddForce After using parameters GetxxxDown It can also work ? First, according to logic , Use GetxxxDown It will only be executed for a short time , Therefore, the effect of parameters should work in an instant, rather than gradually changing with the implementation .

Unfortunately, the official documents do not explain this ForceMode Some characteristics and differences of . The above pictures are translated as : The acceleration , impulse , Speed change . After testing, impulse and speed changes can take effect instantly .
- For the third case , In official API It is described in :

It should be noted that the active state here does not mean that the object needs to move , It's the object itself Setactive attribute .
- For the last logical problem mentioned by the author , There are many ways to write code , Can't take into account all possible situations , The author here shares the method of personal testing process , The first is to use more Debug.Log Method , Used to judge whether the code is executed smoothly , The second is to delete , Delete some logic , Test again if the function is normal , This method should be very easy to use for finding problems .
If it helps you , Can you like it ~(●ˇ∀ˇ●)
边栏推荐
- Sword finger offer 55 - ii balanced binary tree
- Sword finger offer 55 - I. depth of binary tree
- putchar()
- Tableau JDBC连接GraphDB
- 博客从 CloudBase 迁移至云主机
- Daily question brushing record (XXV)
- Mathematical method - random integer in the packaging range - six digit random verification code - random color string - decimal conversion and decimal places retention
- R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化经验累积密度分布函数曲线(Empirical cumulative density function)
- Gd32f4 (6): serial port garbled caused by crystal oscillator
- [JS encapsulates a simple asynchronous API to obtain asynchronous operation results and process parsing]
猜你喜欢

uniapp Request请求封装的方法

剑指 Offer 54. 二叉搜索树的第k大节点

Amaslab-epic-kbs9 industrial computer brushing document

高数下|二重积分的计算1|高数叔|手写笔记

I2C通信协议实现在OLED显示屏数据显示

(手工)【sqli-labs54-57】限制注入次数:联合注入、报错回显、GET注入
![[cache] introduction of a new cache caffeine cache](/img/0c/cd672209c36d53e9833f239a463ed1.png)
[cache] introduction of a new cache caffeine cache

Play through ansible directory in one step

C language as a push box

On array method reconstruction and re encapsulation -foreach map -- push (), unshift (), shift (), map (), filter (), every (), some (), reduce ()
随机推荐
sentinel
数据统计分析案例(对比分析、销量定比分析、同比、双坐标图、环比、shift、贡献度分析(帕累托法则)、差异化分析、resample、季节性波动分析)
Part 13: implementation of STM32 I2C serial bus communication
R language uses the designtreatmentsc function of vtreat package to build a data processing plan, and uses vtreat package for data preparation
(manual) [sqli-labs54-57] limit the number of injections: joint injection, error echo, get injection
Amaslab-epic-kbs9 industrial computer brushing document
激活navicat提示rsa public key not find的问题
知识图谱de构建与应用(四):知识获取
R language uses data The table package sorts the dataframe row data (sort the data rows based on multiple fields and variables without reordering the actual data changes), and calculates the cumulativ
科技云报道:构建可观测性的核心能力是什么?
On array method reconstruction and re encapsulation -foreach map -- push (), unshift (), shift (), map (), filter (), every (), some (), reduce ()
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型
产业园区如何做好精细化运营管理
Apache log related
[UI app mobile UI framework]
R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize faceted balloon graph (visualization of contingency table composed of two classification variables), fac
R语言使用pcauchy函数生成柯西分布累积分布函数数据、使用plot函数可视化柯西分布累积分布函数数据(Cauchy distribution)
Use of prettier code formatting tool
True question of CCF (anger takes 100 faints)
Pytorch depth separable convolution and mobilenet_ v1