My game depends on dragging two rigid bodies attached to spring joints at once, but the standard dragrigidbody.js doesn't support it. How can I add this functionality?
Here is the one I wrote from this [forum post][1]
using UnityEngine;
using System.Collections;
public class MultiDragRigidbody2D : MonoBehaviour
{
public int maxTouch = 2;
[Range(0,31)]
public int layerMask = 0;
public float distance = 0.2f;
public float dampingRatio = 1;
public float frequency = 1.8f;
public float linearDrag = 1.0f;
public float angularDrag = 5.0f;
public bool centerOfMass = false;
private SpringJoint2D[] springJoints;
void Start ()
{
springJoints = new SpringJoint2D[maxTouch];
for (int i = 0; i < maxTouch; i++) {
GameObject go = new GameObject ("Dragger" + (i + 1));
go.transform.parent = this.transform;
Rigidbody2D body = go.AddComponent ("Rigidbody2D") as Rigidbody2D;
springJoints *= go.AddComponent ("SpringJoint2D") as SpringJoint2D;*
body.isKinematic = true;
}
}
void Update ()
{
foreach (Touch touch in Input.touches) {
int Id = touch.fingerId;
if (Id < maxTouch && touch.phase == TouchPhase.Began) {
Camera mainCamera = FindCamera ();
Ray ray = mainCamera.ScreenPointToRay (touch.position);
RaycastHit2D hit = Physics2D.GetRayIntersection (ray, Mathf.Infinity, 1 << layerMask);
if (hit.rigidbody != null && hit.rigidbody.isKinematic == false) {
springJoints [Id].transform.position = hit.point;
springJoints [Id].connectedBody = hit.rigidbody;
if (centerOfMass)
springJoints [Id].connectedAnchor = hit.rigidbody.centerOfMass;
else
springJoints [Id].connectedAnchor = hit.transform.InverseTransformPoint (hit.point);
float length = (hit.transform.position - mainCamera.transform.position).magnitude;
StartCoroutine (DragObject (Id, length));
}
}
}
}
IEnumerator DragObject (int Id, float length)
{
float oldDrag = springJoints [Id].connectedBody.drag;
float oldAngularDrag = springJoints [Id].connectedBody.angularDrag;
springJoints [Id].distance = distance;
springJoints [Id].dampingRatio = dampingRatio;
springJoints [Id].frequency = frequency;
springJoints [Id].connectedBody.drag = linearDrag;
springJoints [Id].connectedBody.angularDrag = angularDrag;
Camera mainCamera = FindCamera ();
while (true) {
bool touchExists = false;
foreach (Touch touch in Input.touches) {
if (touch.fingerId == Id) {
touchExists = true;
Ray ray = mainCamera.ScreenPointToRay (touch.position);
springJoints [Id].transform.position = ray.GetPoint (length);
}
}
if (touchExists)
yield return null;
else
break;
}
if (springJoints [Id].connectedBody) {
springJoints [Id].connectedBody.drag = oldDrag;
springJoints [Id].connectedBody.angularDrag = oldAngularDrag;
springJoints [Id].connectedBody = null;
}
}
Camera FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
}
[1]: http://forum.unity3d.com/threads/rigidbody2d-dragable-script.212168/#post-1664297