- Home /
Dragging a 2D object that has a hinge or spring joint
This is pretty simple, I suppose. I have a cat object made of several sprites (arms, legs, head etc). They are all sprites with a Rigidbody and Poly collider. They are all attached together with Hinge joints (I have another with Spring but doesn't work as well).
I attach a Mouse/Touch drag script to the parent Cat object. It works fine. I can click and move the cat around. But as soon as I do so, it does not maintain the distances defined for my hinge joints (which are as small as allowed). I end up with a cat split into many pieces (oh god!) until I unclick and he flies back together. This is the script I am using that I found for Drag.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PolygonCollider2D))]
public class Drag : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
I'm just wondering how to make this more like a ragdoll. I cant seem to get the hinge joints to maintain their distance while I drag one of the sprites.