Change a line composed of vextor3's to a constant.force on an object

Hello and thanks for your attention.

I'd like to use a line composed of vector3 vertex positions to apply force to objects, via constant.force, that intersect it.

The fiction is: think of a leaf getting caught up in a current of water in an otherwise still pond.

The scripting documentation on constant force is a little light. Can I take line segments defined by vextor3's and apply them as (appropriate to fiction) constant forces on objects?

Thanks!

--Goody!

Well, you probably want to check how close the leaf is to the "water-line", and only apply the force if the leaf is sufficiently close. You can write a function that calculates the distance between a line segment and a point.

However, rather than doing this 100% through scripting, you might want to consider this approach instead:

Make a box trigger object. Scale it to be thin and long, almost like a line, except that you can control the thickness. You could make it long along the Z axis and thin in the X and Y axis.

Now make a script on this line with a OnTriggerStay function that applies a force to the GameObject (leaf) that hit the trigger - you can just apply the force along the z axis of the object.

This way you can setup the position and alignment of the "water-line" in the Scene view, and also control the exact width. Furthermore, it requires much less math than if you wanted to script it all, and is also more efficient if you have many "water-lines" at once (unless you know how to optimize the code properly, which is even more work).

First have a look at the documentation for Rigidbody.AddForce.

Note that the first parameter is a vector along which the force is to be applied. This force will be applied at the object's center of gravity - it won't start spinning, just moving in the indicated direction.

To apply a force that is offset from the center of gravity, see Rigidbody.AddForceAtPosition. This will move and spin your object. The "position" parameter in this case might be the point on the "force line" that is closest to your object, but you can probably get away with using other positions, for example, if the points defining your "force path" are close together, you might be able to use the nearest one.

To get the direction to apply the force, subtract the first point (A) (of the force line segment) from the next point along the path (B). This gives you a vector that represents the direction from point A to point B.