Whenever I add a new HingeJoint2D (or any joint for that matter) component on any object and connect it to another rigidbody through a script, it doesn't fully connect the two objects together in a way that's hard to explain. It's like there's some sort of distance set between the two objects before they can interact with the hinge. Does anyone know what the issue is and how I could solve it?
#Joints are acting strange
1 messages · Page 1 of 1 (latest)
Show your code?
Are you setting all of these properly?
https://docs.unity3d.com/6000.3/Documentation/ScriptReference/AnchoredJoint2D-anchor.html
https://docs.unity3d.com/6000.3/Documentation/ScriptReference/AnchoredJoint2D-connectedAnchor.html
using UnityEngine.InputSystem;
public class NewGrabController : MonoBehaviour
{
[SerializeField] private GameObject grabbingTarget;
private bool isGrabbing = false;
private void OnCollisionEnter2D(Collision2D col)
{
grabbingTarget = col.gameObject;
}
private void OnCollisionExit2D(Collision2D col)
{
if (isGrabbing) return;
if (col.gameObject == grabbingTarget)
{
grabbingTarget = null;
}
}
public void Grab(InputAction.CallbackContext context)
{
if (grabbingTarget != null && context.performed)
{
isGrabbing = true;
Rigidbody2D rb = grabbingTarget.GetComponent<Rigidbody2D>();
if (rb != null)
{
HingeJoint2D hingeJoint = gameObject.AddComponent(typeof(HingeJoint2D)) as HingeJoint2D;
hingeJoint.connectedBody = rb;
hingeJoint.breakForce = 1200;
}
}
if (!context.performed && GetComponent<HingeJoint2D>() != null)
{
Destroy(GetComponent<HingeJoint2D>());
isGrabbing = false;
}
}
}```
This is meant to be a grab script for an active ragdoll
you're not setting the anchors at all
Should I be? A few weeks ago this code worked fine.
if you want to control exactly where on each object the joint is attached, yes.
If you don't, it will be attached at the pivot point of each object (local 0,0,0)
the problem is the joint doesn't seem to attach to either of the two objects properly. They kinda "pull" each other together as if a grappling hook was being pulled but they don't "connect"
that doesn't seem to be the case. The anchor is at the pivot point of the hands, but the connected anchor is located wherever the collision took place