Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Goody! · Nov 18, 2009 at 03:20 AM · physicsflow

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Goody! · Nov 21, 2009 at 11:49 PM 0
Share

Nailed it! Subtracting a from b seems to do the trick! Thanks!

avatar image
1

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).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Goody! · Nov 19, 2009 at 01:37 AM 0
Share

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?

avatar image Bampf · Nov 20, 2009 at 04:01 PM 0
Share

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.

avatar image Goody! · Nov 21, 2009 at 11:53 PM 0
Share

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? :)

avatar image runevision ♦♦ · Nov 23, 2009 at 09:47 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

hit.rigidbody.useGravity problem? 1 Answer

Destroy parts of a Wall 2 Answers

Physics: Is it possible to count the number of colliders hit with OverlapSphere? 1 Answer

Make car CenterOfMass change dynamically 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges