为什么Unity2D的Physics2D引擎中的 Gravity 在交替实现这两行代码时会有所不同?
例如,我已将我的播放器精灵附加到播放器控制器C#脚本:
private float speed = 500f;
RigidBody2D playerChar = null;
然后让我的角色走:
Vector2 vec = new Vector2 (Input.GetAxis("Horizontal"), 0);
playerChar.AddForce(vec * speed);
重力设定为50
结果1:我的角色头像正常掉落。
与此同时:
Vector2 vec = new Vector2 (Input.GetAxis("Horizontal"), 0);
playerChar.velocity = (vec * speed);
重力仍然设定为50
结果2:我的角色现在需要很长时间才会摔倒(它慢慢地“漂浮”下来)。
为什么?
答案 0 :(得分:3)
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 a y 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. Then Update happens again and you force the y value back to 0 once more.