- Home /
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!
Answer by Bampf · Nov 19, 2009 at 02:36 AM
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.
Nailed it! Subtracting a from b seems to do the trick! Thanks!
Answer by runevision · Nov 18, 2009 at 02:11 PM
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).
I think for the leaf interacting with the line I'm going to try fiddling with larger or smaller than you'd expect collision. Thanks for the suggestion but the line can, and most likely will, curve multiple times as the user will be defining it with a touch drag on an Iphone screen. I'll re-ask my original question another way: Is there a way to apply a constant force on an object that's "push direction" is defined by two pre-existing vextor3's?
Thinking about Rune's approach, there's no reason the box triggers couldn't be defined dynamically by your script. (Although to do so you still need same math.) With this approach you could create a static test case in the editor; once working, then create the boxes dynamically. Your test case should try a lot of boxes, just to make sure it'll perform... I think in the end both approaches are a similar amount of work, but for a game with static paths that can be created ahead of time in the editor, Rune's approach might be easier.
I've considered that approach as well but I worry that I'll be drawing 50+ little colliders and kill frame rate. If I can't get it going with raycasting I'll try that next. Rune, any idea how to detect when it's near the line andd draw it in to the center of the line? Two or three lines running parallel? :)
If you want to go the non-box approach, you can follow the link I just added in the answer. It has some line segment to point distance solutions. As for drawing the leaf closer to the line, that's a closely related problem of finding the closest point on a line segment, and then adding a force that pushes it towards that point.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
SimpleMove Jump 1 Answer
Throwing Object with acceleration equation/script? 2 Answers
Shooting a projectile forward in 3d space 3 Answers
Engine noises creation 1 Answer