Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by AndersWR · Oct 06, 2013 at 05:35 PM · physicsrigidbodyforce

Use force to move to position of object

Hello there.

What I'm looking to achieve, is to make a rigidbody follow another gameobjects position, and I want the rigidbody to travel faster if the distance between the rigidbody and the gameobject is less.

I tried using a spring joint, but I realized that I need it to work the opposite way as a spring joint, so instead it would move faster the closer to the connected body the rigidbody is.

I've experimented with configurable joint, but I can't seem to understand how to do what I want with that method.

I've been looking into adding constant force directly in the direction of the object:

Vector3 Direction = (GameobjectA.transform.position - gameObject.transform.position).normalized; rigidbody.constantForce.force = Direction;

I've been trying to modify the rigidbody.velocity directly, but I realized that I should not do that, as I need my physics objects to work correctly

So at the moment I'm wondering what method I should try and use. I'm not very experienced with maths and physics, and I've currently run out of options to try. I think adding force in the direction of the gameobject would be the way to go, but with what I have so far the rigidbody sort of bounces forth and back trying to reach the target position, and I'm not sure how to fix that without modifying the velocity directly or slow the rigidbody down when its closer, as I need the opposite to happen.

Note that my gameobject is empty, and my rigidbody is simply trying to reach its position with force.

Any help or ideas would be greatly appreciated, thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Oct 06, 2013 at 08:34 PM

I'm not sure what you are going for here. You can modify the velocity based on distance. Here is a bit of code that, when the target is in range, chases the target. The closer it is, the faster it goes:

 var target : Transform;
 var maxDist = 5.0;
 var maxVelocity = 8.0;
 
 function FixedUpdate () {
     var dist = Vector3.Distance(target.position, transform.position);
     if (dist < maxDist) {
         rigidbody.velocity = (target.transform.position - transform.position).normalized     * (maxVelocity * (1.0 - dist / maxDist));
     }    
     else {
         rigidbody.velocity = Vector3.zero;
     }
 }
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 AndersWR · Oct 07, 2013 at 06:50 AM 0
Share

Hello thanks for your answer! The thing is that I need my rigidbody to work physically correct with the physics. That's why I'm trying to move my rigidbody with forces, and if I modify the rigidbody.velocity directly, wouldn't this mean that the rigidbody will no longer collide properly etc?

I'm wondering if ins$$anonymous$$d of setting the velocity to zero when it's closer, if I ins$$anonymous$$d should use additional forces to bring it back in position. When dealing with forces like in my example above, as the rigidbody chases the gameobject, the problem occours when the rigidbody reaches target position, it naturally passes through the gameobject and applies forces to the rigidbody to try and reach its position again. This causes a sort of bouncing effect. So I'm wondering if there is a way to stop the rigidbody without modifying the velocity directly, and ins$$anonymous$$d stop the gameobject with forces.

I just thought of a solution to try out. What if I check the direction of the rigidbody and the distance, and if it's moving away from the gameobject, then apply forces in the right direction based on the velocity of the rigidbody, and the distance from the gameobject. So basically I would try and hold the rigidbody in one spot by putting forces from all directions. Note that I'm also using gravity on my rigidbody.

I will try and implement this today and I'll see how it goes, but mostly I'm wondering if I'm even going in the right direction here, and if what I've questioned with modifying the velocity of the rigidbody directly is true

Thanks again, and sorry for the poor explanation

avatar image robertbu · Oct 07, 2013 at 04:04 PM 1
Share

Hello thanks for your answer! The thing is that I need my rigidbody to work physically correct with the physics. That's why I'm trying to move my rigidbody with forces, and if I modify the rigidbody.velocity directly, wouldn't this mean that the rigidbody will no longer collide properly etc?

No. The physics will still work if you modify the velocity directly.

I'm wondering if ins$$anonymous$$d of setting the velocity to zero when it's closer, if I ins$$anonymous$$d should use additional forces to bring it back in position.

You could use additional force, but to make it work, you would need significant drag. AddForce() does not replace the current velocity, it just modifies it. Think space ship. If it is traveling in one direction and you apply a side force, the ship will not turn 90 degrees, but ins$$anonymous$$d angle off. This can make it difficult to aim your object unless you have enough drag to decay the existing velocity and enough new velocity to set the new direction.

it naturally passes through the gameobject and applies forces to the rigidbody to try and reach its position again. This causes a sort of bouncing effect. So I'm wondering if there is a way to stop the rigidbody without modifying the velocity directly, and ins$$anonymous$$d stop the gameobject with forces.

Again, modifying the velocity does not disable/hurt physics. You could create a counter force to stop the object, but that is equivalent to setting the velocity to zero.

avatar image AndersWR · Oct 07, 2013 at 04:52 PM 0
Share

Thank you so much for taking your time :)

I must say I'm a bit confused regarding the direct velocity changes. The docs says that in most cases you should not modify the velocity directly as it can result in unrealistic behaviour

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html

Does this mean that the docs are wrong, or should I use forces if I want 100% realistic behaviour? I've been experimenting and testing for a few hours now and I'm getting a lot closer to getting it to work with forces, although it would be a lot easier to just change the velocity directly if that works 100% realistically as well. Thanks again

avatar image robertbu · Oct 07, 2013 at 05:31 PM 0
Share

The Doc is right to a point. But:

  • There are lots of games that have unrealistic physics that are played all the time.

  • It is possible to create unrealistic physics using AddForce(). For example, if you set the drag in the Rigidbody high, and apply force every frame, then the effect will be very similar to setting the velocity.

  • It is possible to make velocity more realistic through a bit of vector math.

What I'm saying is don't use AddForce()for this problem just because you think it is better by some mythical standard mentioned in the manual. Use it if it gives you something...feels better in terms of motion for example.

I don't know the nature of your game nor the game mechanic you are ai$$anonymous$$g at, so it is difficult to advise about AddForce(). I think with AddForce() you will struggle to have the following object hit the target. Upping the drag will help significantly.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Predicting land position with drag applied 0 Answers

Calculating required force for pushing a body to a desired position at once 0 Answers

Tossing an object with the correct force at any distance from another so that it collides with the other 1 Answer

increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer

Tornado throwing back force after pulling 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