简体   繁体   English

RigidBody2D冻结X位置

[英]RigidBody2D freeze X position

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. 我想知道是否有一种方法可以使用Unity的RigidBody2D模拟以下代码行,而不是使用普通的RigidBody。

rigidbody.constraints = RigidbodyConstraints.FreezePositionX;

I wanting my players x position to freeze when it collides with something. 我希望我的球员x位置在碰到某些东西时冻结。 Whilst I could use the above, it would require I rework all my 2D collisions to work with the 3D collision. 虽然我可以使用上述内容,但我需要将所有2D碰撞重新加工以处理3D碰撞。 A pain I'd rather avoid. 我宁愿避免的痛苦。

This is due to the Box2D engine use to do the simulation. 这是由于Box2D引擎用来做模拟。 It does not directly provide a constraint on the rigid body itself. 它不直接对刚体本身提供约束。 It does however provide joints. 然而它确实提供关节。

What you want to look into is a Slider Joint 2D . 您想要了解的是Slider Joint 2D This will allow you to constrain movement in a particular direction. 这将允许您限制特定方向的移动。

By default it will freeze/constrain the X position (ie you can just move up or down). 默认情况下,它会冻结/约束X位置(即您可以向上或向下移动)。 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: 在这种情况下,您添加一个如下所示的滑块关节:

This will allow the specific 2D Rigid Body to only move up or down. 这将允许特定的2D刚体仅向上或向下移动。 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). 如果不这样做,它将在原点(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. 可以通过适当更改“连接锚点”设置来修改此设置。

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. 将它设置为90度就可以了。

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 . 但是为了帮助你,我在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. 它不会做太多,但通过与场景视图中的两个方块进行交互(尝试沿着轴进行平移),您可以了解它正在做什么。

Here is a script component I use in Unity2D to lock the axis on any object. 这是我在Unity2D中使用的脚本组件,用于锁定任何对象上的轴。 Just attach the script, choose an axis from the dropdown, and you should be good to go. 只需附上脚本,从下拉列表中选择一个轴,你就应该好好去。 Note your object will need a rigidbody2d and collider to work properly of course. 注意你的对象当然需要一个rigidbody2d和collider才能正常工作。

Thanks to @Bart for a great answer on how Slider Joint 2D works. 感谢@Bart对Slider Joint 2D如何工作的一个很好的答案。 If this script doesn't make sense see his answer. 如果这个脚本没有意义,请看他的答案。

using UnityEngine;
using System.Collections;

enum AxisDirection {
    x, y
}

[RequireComponent (typeof (SliderJoint2D))]
public class AxisLock : MonoBehaviour {
    [SerializeField] AxisDirection lockAxis;

    void Awake () {
        SliderJoint2D slider = GetComponent<SliderJoint2D>();

        slider.connectedAnchor = new Vector2(transform.position.x, transform.position.y);
        slider.collideConnected = true;

        if (lockAxis == AxisDirection.x) {
            slider.angle = 90;
        } else {
            slider.angle = 0;
        }
    }
}

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

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