- Home /
DragRigidbody Scroll in and out
okay so, i've got the Drag Rigidbody script from the standard assets, now how can I make it so when you scroll it translates forward and backward :)
Answer by robertbu · Oct 15, 2013 at 03:42 PM
By "scroll" do you mean the scroll wheel? Regardless, the trick is to change the 'distance' used in the DragObject() function. For example, I've added this line to change the position based on the scroll wheel:
distance += Input.GetAxis("Mouse ScrollWheel"); // <<<<<<<<<<
Whole function:
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))
{
distance += Input.GetAxis("Mouse ScrollWheel"); // <<<<<<<<<<
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;
}
}
You probably want to put some limits on the modification of distance...clamp it to the near and far clip planes at least.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115957im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Excluding a child object from also being drag-able 0 Answers
NGUI: Draggable Camera and Scroll Panel 0 Answers
Nested Scroll Views: How can I pass drag control from an inner scroll view to its outer scroll view? 2 Answers
Is it possible to drag UIImage out of ScrollRect? 2 Answers
DragRigidbody - move object in Local Space, not World Space 1 Answer