Locking 2D screen points along a specififed ray

This is a basic question that I'm sure has been answered before, but I couldn't find what I was looking for by searching...

I have two game objects in the 3d space. I want to be able to use the mouse to drag and throw (move) one object towards the other. I thought I would start by creating a ray from one object to the other. Thats easy enough. But how would I then lock the movement of the object so that it only follows the along the ray when you drag it with say a mouse (for now)? I have looked at Camera.SceenPointToRay() and similar functions, but I'm still out of ideas.

Would someone care to point my in the right direction?


The behavior you describe won't be entirely straightforward to implement, but basically what you want to do is constrain the motion of the first object so that it lies on a line passing through the positions of both objects, and then move the object in 'line space' (e.g. parametric space) in response to mouse motions.

One way to compute the parameters of the line is as follows (untested pseudocode):

direction = normalize(targetObject.position - object.position);
origin = object.position;

The position of the first object in parametric space is initially 0. For moving the object along the line, if you want a 'throwing' effect, you can use simple physics (velocity and acceleration), but in 1-d instead of 2-d or 3-d.

Here's an alternate approach that might be a little easier to manage. Instead of moving in parametric space, move the object as you would normally, and then move the object to the position corresponding to the orthogonal projection of the current position onto the aforementioned line. (This will have the effect that the object motion appears to be constrained to the line.)