如何在Unity中为2D对象添加向上方向的力量

huangapple go评论67阅读模式
英文:

How to add force in up direction of a 2d object in Unity

问题

如何在2D物体旋转后向上方施加力?我正在使用AddForce(transform.up),但它总是将物体向上移动,不考虑其旋转。
我想在船只旋转后仍然沿着其前进方向移动。

英文:

How to add force in up direction of a 2d object after it has rotated? I am using AddForce(transform.up) but it always moves the object upwards, irrespective of its rotation.
I want to move a ship always in its forward direction even after it spins.

答案1

得分: 0

When using a Rigidbody, you are agreeing to no longer modify the transform directly, and let it be controlled only by the rigidbody. By rotating using the transform you are not properly allowing the body to have angular momentum, and can run into a host of different issues.

Instead, rotate your object through Rigidbody.MoveRotation(), or better: Rigidbody.AddTorque()

You can also try adding the force relative, this should give you the correct direction:

rb.AddRelativeForce(Vector3.Up);

Which is guaranteed to apply the force in the direction the rigidbody is facing. If you have the same behavior with this then the object with your rigidbody is not being rotated and therefore world and local up will be the same.

英文:

[Edit to include actual solution]

When using a Rigidbody you are agreeing to no longer modify the transform directly, and let it be controlled only by the rigidbody. By rotating using the transform you are not properly allowing the body to have angulart momentum, and can run into a host of differnet issues.

Instead, rotate your object through Rigidbody.MoveRotation(), or better: Rigidbody.AddTorque()


<s>it's likely you're not actually rotating the transform and are instead rotating the sprite or a child of the transform. You can verify this by using:
</s>

You can also try adding the force relative, this should give you the correct direction:

rb.AddRelativeForce(Vector3.Up);

<s>
Which is guaranteed to apply the force in the direction the rigidbody is facing. If you have the same behaviour with this then the object with your rigidbody is not being rotated and therefore world and local up will be the same.

</s>

huangapple
  • 本文由 发表于 2023年5月11日 02:27:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定