سؤال

I'm wondering if it's somehow possible to find the Joint that holds my Rigidbody, through the rigidbody?

Allow me to clarify more I have my GameObject and it has a FixedJoint which has a Rigidbody attached to it (the connectedBody) and what I want to do is to find the FixedJoint, by looking for it from the Rigidbody.

Reason to why I want to do this is because I want my Joint to break when my Rigidbody hits something hard enough. I can't use BreakForce as it's checked all the time, so if I turn my original object fast enough it will break.

هل كانت مفيدة؟

المحلول

Through the Rigidbody, unfortunately there isn't. Given the FixedJoint, you can check its connectedBody property, but you can't check it the other way around.

Don't forget, though, that you can always attach a custom component.

Something like this would be simple enough:

public class FixedJointConnection : MonoBehaviour {
    public FixedJoint joint;
}

When you attach the joint, you can also attach a FixedJointConnection and set the joint reference.

You can even write a helper function to do just that:

public static FixedJointConnection Connect(Rigidbody anchor, Rigidbody child) {
    FixedJoint joint = child.AddComponent<FixedJoint>();
    joint.connectedBody = anchor;
    joint.connectedAnchor = anchor.transform.InverseTransformPoint(child.transform.position);

    //TODO: any more joint config? depends on your game

    FixedJointConnection connection = anchor.AddComponent<FixedJointConnection>();
    connection.joint = joint;

    return connection;
}

From there, you can add a check during each collision:

  • Do we have any FixedJointConnection(s) attached?
  • If so, did we hit hard enough to break them?

Depending on what exactly you need, your custom component class can be very simple or fairly complex. Doesn't matter, though; the point here is just that Unity's components provide powerful features, and that you can amplify those features by writing your own.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top