英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论