RigidBody2D冻结X位置 [英] RigidBody2D freeze X position

查看:1882
本文介绍了RigidBody2D冻结X位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有效仿使用Unity的RigidBody2D相对于使用普通的刚体下面的代码行的方式。

I'm wondering if there is a way to emulate the following line of code using Unity's RigidBody2D as opposed to using a normal RigidBody.

rigidbody.constraints = RigidbodyConstraints.FreezePositionX;



我想我的球员们x位置时,它的东西碰撞冻结。虽然我可以用上面的,它需要我返工我所有的2D碰撞与3D碰撞工作。一个痛苦,我宁愿避免。

I wanting my players x position to freeze when it collides with something. Whilst I could use the above, it would require I rework all my 2D collisions to work with the 3D collision. A pain I'd rather avoid.

推荐答案

这是由于发动机的Box2D用做模拟。它不直接提供在刚性体本身的约束。但它确实提供了关节。

This is due to the Box2D engine use to do the simulation. It does not directly provide a constraint on the rigid body itself. It does however provide joints.

您想看看什么是一个滑块联合2D 。这将允许你限制在一个特定的方向运动。

What you want to look into is a Slider Joint 2D. This will allow you to constrain movement in a particular direction.

默认情况下将冻结/约束X位置(也就是你可以向上或向下移动)。通过修改的角度,可以改变沿着该物体可以移动该行。

By default it will freeze/constrain the X position (i.e. you can just move up or down). By modifying the angle, you can change the line along which the object is allowed to move.

因此,让我们说你要垂直移动约束。在这种情况下,添加一个滑动接头,看起来像这样:

So let's say you want to constrain movement vertically. In that case, you add a slider joint that looks like this:

这将允许特定的2D刚体只向上或向下移动。有几件事情,这里要注意。接头相对于其他刚体,你通常会添加到连接刚体工作。如果你不这样做,它会隐设立一个在原点(0,0)。这将有抢购你的身体约束在那里,当你可能不希望它的效果。这可以通过适当地改变连接锚的设置进行修改。

This will allow the specific 2D Rigid Body to only move up or down. There are a couple of things to note here. Joints work in relation to other rigid bodies, which you would normally add to the "Connected Rigid Body". If you don't, it will implicitly set up one at the origin (0,0). This will have the effect of snapping your constrained body there, when you might not expect it. This can be modified by changing the "Connected Anchor" settings appropriately.

如果你希望你的约束刚体水平你会做和以前一样,加上一个角度。将其设置为90度,会做的伎俩。

If you wish to constrain your rigid body horizontally you'd do the same as before, with the addition of an angle. Setting it to 90 degrees will do the trick.

这怎么适合自己的设置和代码,你不得不设法弄清楚。但是,为了帮助你走好我在Github上 在创建了一个小演示现场。

How this fits into your specific setup and code you'd have to try and figure out. But to help you along I've created a small demo scene at over on Github.

这不会做太多,但通过与现场查看两个平方(尝试沿着轴线平移他们),你得到它做什么的想法互动。

It won't do much, but by interacting with the two squares in the scene view (try translating them along an axis) you get the idea what it's doing.

这篇关于RigidBody2D冻结X位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆