- Home /
Click and Drag without rotation
I have the DragRigidBody script attached to the object I wish to click and drag but it rotates, does someone know how to get the rotation out this object.
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; }
Thanks
Answer by Mike 3 · Dec 05, 2010 at 03:31 PM
Doesn't look like the script rotates anything, try ticking Freeze Rotation on the rigidbody
Edit:
function DragObject (distance : float) { rigidbody.freezeRotation = true; 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; } rigidbody.freezeRotation = false;
}
That freezes the total rotation of the object I want my rotation turned off only when I drag
you mean lik this?
rigidbody.freezeRotation = true;
StartCoroutine ("DragObject", hit.distance);
rigidbody.freezeRotation = false;
No, inside DragObject (The coroutine itself), at the top and bottom
could you please show me in the script, I dont understand it anymore,... I'm sorry for being a noob
Answer by pravin gate · Mar 18, 2011 at 11:26 AM
You may just need to apply box collider,charchter controller,rigidbody components. then it will work as u want.
or u can use this script also instead of all this big script:
var screenSpace; var offset;
function OnMouseDown(){
screenSpace = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
}
function OnMouseDrag () {
//keep track of the mouse position var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//update the position of the object in the world transform.position = curPosition;
} and just attach to ur object.
Your answer
Follow this Question
Related Questions
Getting to move object with mouse position? 0 Answers
drag object including free rotation 0 Answers
Click but not drag 1 Answer
OnMouseUp() Click Display Effect. 1 Answer
I am trying to pick up an Object and drag it with my mouse. 1 Answer