简体   繁体   English

在Unity2D中锁定2D精灵的位置

[英]Locking the position of a 2D sprite in Unity2D

I'm trying to lock the position (both x and y) of a sprite in Unity2D. 我试图锁定Unity2D中子画面的位置(x和y)。 I know that Rigidbody3D has constrain features, but Unity2D only has a "Fixed Angle" option (I'm making gears that mesh together and turn other gears so obviously I still want them to be able to turn). 我知道Rigidbody3D具有约束功能,但是Unity2D仅具有“固定角度”选项(我正在制造啮合在一起的齿轮并转动其他齿轮,因此显然我仍然希望它们能够转动)。 Is there anyway to fix a sprite into place? 反正有将精灵固定到位吗?

Thanks 谢谢

rigidBody2D.isKinematic=true;

To quote from the unity manual: 引用统一手册:

The Is Kinematic setting switches off the physical behaviour of the Rigidbody 2D so that it will not react to gravity and collisions. “运动学”设置会关闭“刚体2D”的物理行为,以使它不会对重力和碰撞产生反应。 This is typically used to keep an object under non-physical script control most of the time but then switch to physics in a particular situation. 通常,这通常用于使对象在大多数情况下处于非物理脚本控制之下,但随后在特定情况下切换为物理。

Therefore gravity doesnt affect it, collisions don't affect it. 因此,重力不会影响它,碰撞不会影响它。 You can still move it using scripts, but it won't move via the physics engine. 您仍然可以使用脚本来移动它,但是不会通过物理引擎移动它。

Sounds like you may want the 'position-locked' game object to be kinematic. 听起来您可能希望“位置锁定”游戏对象具有运动学性。 Kinematic rigid bodies cannot be moved by forces. 运动刚体不能通过力移动。

I'm not sure if this is an option in previous versions of Unity, but Rigidbody2D components come with the 'Is Kinematic' flag without any editor customization. 我不确定在Unity的早期版本中是否可以选择此选项,但是Rigidbody2D组件带有'Is Kinematic'标志,而没有任何编辑器自定义。 So this can be done without creating scripts. 因此,无需创建脚本即可完成此操作。

In Unity 5: 在Unity 5中: Rigidbody2D是运动标志

Unity Rigidbody2D Doc: https://docs.unity3d.com/Manual/class-Rigidbody2D.html Unity Rigidbody2D文档: https ://docs.unity3d.com/Manual/class-Rigidbody2D.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM