简体   繁体   English

为什么Physics2D会为AddForce()与速度产生不同的结果?

[英]Why does Physics2D produce different results for AddForce() versus velocity?

Why does Gravity in Physics2D engine for Unity2D act different when these two following lines of codes are implemented alternately? 为什么重力在Physics2D发动机Unity2D行动不同的代码时,这两个以下行交替地执行?

For example, I have attached to my player sprite a Player Controller C# script: 例如,我已经附加到我的播放器精灵播放器控制器C#脚本:

private float speed = 500f;
RigidBody2D playerChar = null;

And then to make my character walk : 然后让我的角色走路

Vector2 vec = new Vector2 (Input.GetAxis("Horizontal"), 0);
playerChar.AddForce(vec * speed);

And the Gravity is set to 50 并且重力设置为50

Result 1: My character avatar falls down normally. 结果1:我的角色头像正常掉落。

Meanwhile when I do: 与此同时我做:

Vector2 vec = new Vector2 (Input.GetAxis("Horizontal"), 0);
playerChar.velocity  = (vec * speed);

And the Gravity is still set to 50 并且重力仍然设定为50

Result 2: My character now takes a long time to fall (it slowly "floats" down). 结果2:我的角色现在需要很长时间才能摔倒(慢慢地“漂浮”下来)。

Why is that? 这是为什么?

This is because you're forcing the y component of the rigidbody's velocity to zero. 这是因为你强迫刚体速度的y分量为零。

When you add force, it adds , it doesn't replace. 添加力时,它会添加 ,但不会替换。

When you set the velocity, you're specifically setting it to a Vector2 that has ay value of 0, gravity then kicks in on the fixed update cycle and adds a small amount of gravity, causing your player to fall slowly. 当你设置速度时,你专门将它设置为一个值为0的Vector2,重力然后在固定的更新周期开始并增加一点重力,导致你的玩家缓慢下降。 Then Update happens again and you force the y value back to 0 once more. 然后再次发生更新,并再次强制y值回到0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Physical2D检测2个对撞机的碰撞 - Detecting collision for 2 colliders using physics2d 为什么会出现此错误:Assets\\Character2DController.cs(35,21): error CS0103: The name 'Physics2d' does not exist in the current context - Why is this error appearing: Assets\Character2DController.cs(35,21): error CS0103: The name 'Physics2d' does not exist in the current context 与 3d 相比,为什么 AddForce 需要不同的 2d 语法? - Why does AddForce require different syntax for 2d compared to 3d? 为什么这个 LinqPad 程序在第二次运行时会产生不同的结果? - Why does this LinqPad program produce different results on the second run? 为什么这个表达式在 C# 和 C++ 中产生不同的结果? - Why does this expression produce different results in C# and C++? 为什么完全相同的代码产生不同的结果? - Why does the exact same code produce different results? Velocity和AddForce()在Unity中不起作用 - Velocity and AddForce() are not working in Unity 为什么 Double.ToString("#") 和 BigInteger.ToString() 对相同的值产生不同的结果? - Why does Double.ToString("#") and BigInteger.ToString() produce different results for the same value? 为什么我的Vertex Animation着色器在“场景”和“游戏”视图中产生不同的结果? - Why does my Vertex Animation shader produce different results in Scene and Game view? 如何在 Unity 3D 中使用 AddForce 和 ForceMode.VelocityChange 来限制速度 - How to cap velocity in Unity 3D using AddForce with ForceMode.VelocityChange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM