- Home /
click and drag above y coordinate 7
Hi this is my script.
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;
// in de function update als de y-positie hoger is dan 7 dan is de function dragObject true. // is de de y positie lager dan 7 dan is de function dragObject false. // als het blokje heeft gecollide met een ander blok is de function dragObject false.
function Update () { transform.position.z = 0; // Make sure the user pressed the mouse down print(transform.position.y);
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; rigidbody.freezeRotation = true; }
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
function FindCamera () { if (camera) return camera; else return Camera.main; }
and i need an if statement in it
if(rigidbody.transform.position.y > 7) {
}
I want it so placed that if the rigidbody is below y-coordinate 7 the object disconnects from the drag system. Does anyone know where I should place it.