# Can I have a rigidbody continue to calculate physics while being dragged by code?

EDIT2: I was having more difficulty than I should have getting this to work in my game so I set it aside for a while and came back to it and got it to work in 15 minutes. The solution that worked for me was to use a Spring Joint, not a Fixed Joint like the selected answer says.

EDIT: Edited title to remove extra question mark. No idea why, but while typing in the question field in Firefox the entire box gets covered by a black rectangle so I missed the typo.

In my game I have 3D objects being dragged along a 2D plane via touch position, and when the touch ends I want to throw the object with the same velocity/direction of the drag.

While being dragged by code the rigidbody still has collision and all that fun stuff, but it doesn’t do any velocity calculation (I know why, (Unity Physics only calculate when moved by physics forces) but I want to make a work around.)

My first solution was to calculate direction and velocity based on position the tick that the object is let go (touch release) and the tick immediately before (previousPosition), but obviously this calculates based off of positions over 1/30th of a second, so to players it seems unpredictable.

I’m currently working on a solution that stores positions over time, but to do that accurately I need to check and compare each position and only do the final calculation based off of positions that are along a similar path, and I already know this won’t feel quite right either. (Not to mention it is complicated…it involves calculating velocity and direction between each position in the array and discarding bad values (if they stray too far from the average velocity/direction discard those values. I feel that this way would be more accurate than my first solution, but that it’s also too complicated for a relatively simple action.)

Is there a way that the object being dragged can calculate this automatically with Unity’s physics?

If not, is there a better solution to this problem that someone could recommend? I’ve been mulling over this one for a few weeks now and have yet to have an epiphany.

EDIT: I accidentally a comma.

An easy way is to connect the rigidbody with another rigidbody via a fixed joint und just move the other object in FixedUpdate. This enables you to have full collision-detection.

Your “average position” idea isn’t that bad to write. Use a weighted average to adjust speed and direction towards current frame:

``````speed = Mathf.Lerp(speed, speedThisFrame, 0.3f);
dir = Vector3.Lerp(dir, dirThisFrame, 0.3f);
``````

Where you’re already computed speedThisFrame and dirThisFrame. The 0.3 is how quickly it adjusts to changes in direction (like a 1-frame sideways flick at the end.)

Another way to do it is to use physics for the finger drag. Instead of setting position, use physics to “push” the object towards your finger. When it gets close, chop the velocity way down (to avoid bobbling.) Not super-easy to do, but can look really pretty. When you stop dragging, the object will naturally keep flying.