Drag and Drop rigidbody

got scripts? Need scripts?
Post Reply
SweetRevenge
Posts: 52
Joined: Sun Jul 04, 2010 4:53 am

Drag and Drop rigidbody

Post by SweetRevenge »

So I am using the DragRigidBody script that comes with the standard assets. Want I am doing is attaching this to a toy block, and I want to click and drag this block around. That all works, however, the problem I am having is that I want to set up a radius around the object, and you can only manipulate the block when you are inside this radius. Something like a pickupdistance of like 5. Also when I drop the object, it slowly falls to the ground, I want it to fall faster. Can anyone help please?

Code: Select all

var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;

private var springJoint : SpringJoint;

function Update ()
{
	// Make sure the user pressed the mouse down
	if (!Input.GetMouseButtonDown (0))
		return;

	var mainCamera = FindCamera();
		
	// We need to actually hit an object
	var hit : RaycastHit;
	if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, 100))
		return;
	// We need to hit a rigidbody that is not kinematic
	if (!hit.rigidbody || hit.rigidbody.isKinematic)
		return;
	
	if (!springJoint)
	{
		var go = new GameObject("Rigidbody dragger");
		var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
		springJoint = go.AddComponent ("SpringJoint");
		body.isKinematic = true;
	}
	
	springJoint.transform.position = hit.point;
	if (attachToCenterOfMass)
	{
		var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
		anchor = springJoint.transform.InverseTransformPoint(anchor);
		springJoint.anchor = anchor;
	}
	else
	{
		springJoint.anchor = Vector3.zero;
	}
	
	springJoint.spring = spring;
	springJoint.damper = damper;
	springJoint.maxDistance = distance;
	springJoint.connectedBody = hit.rigidbody;
	
	StartCoroutine ("DragObject", hit.distance);
}

function DragObject (distance : float)
{
	var oldDrag = springJoint.connectedBody.drag;
	var oldAngularDrag = springJoint.connectedBody.angularDrag;
	springJoint.connectedBody.drag = drag;
	springJoint.connectedBody.angularDrag = angularDrag;
	var mainCamera = FindCamera();
	while (Input.GetMouseButton (0))
	{
		var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
		springJoint.transform.position = ray.GetPoint(distance);
		yield;
	}
	if (springJoint.connectedBody)
	{
		springJoint.connectedBody.drag = oldDrag;
		springJoint.connectedBody.angularDrag = oldAngularDrag;
		springJoint.connectedBody = null;
	}
}

function FindCamera ()
{
	if (camera)
		return camera;
	else
		return Camera.main;
}
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Drag and Drop rigidbody

Post by hallsofvallhalla »

wondering if you could use either raycast or create a invisible sphere around it.
SweetRevenge
Posts: 52
Joined: Sun Jul 04, 2010 4:53 am

Re: Drag and Drop rigidbody

Post by SweetRevenge »

thats what i was thinking, however i am horrible at writing the script.
SweetRevenge
Posts: 52
Joined: Sun Jul 04, 2010 4:53 am

Re: Drag and Drop rigidbody

Post by SweetRevenge »

I've figured out the radius issue, now I just need a way to increase the fall speed of the objects.
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Drag and Drop rigidbody

Post by hallsofvallhalla »

change their mass or change the gravity.
Post Reply

Return to “Scripting/Coding”