Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 andrew196 · Sep 08, 2018 at 05:16 PM · rigidbody2dphysics2dmousepositionaddforceatposition

Rigidbody2D AddForceAtPosition - adds more force the further away the target

So as the title describes when I implemented the AddForceAtPosition into my code, as shown below, this caused an interesting behavior. What the code does is as the user clicks on the screen the ball should travel away from it, this behavior is weird because the magnitude of the force gets larger as the click is further away from the target. This is the opposite of what I would like as I would think the closer the click to the target the stronger that force should be. Here is the code as described:

 if (Input.GetMouseButtonDown(0)) {
     Vector2 mousePosition =
         new Vector2(Input.mousePosition.x - (Screen.width/2),
                     Input.mousePosition.y - (Screen.height/2));
     
     Vector2 transformPosition =
         new Vector2(transform.position.x,
                     transform.position.y);
 
     GetComponent<Rigidbody2D>()
         .AddForceAtPosition((transformPosition - mousePosition), mousePosition);
 }


Thanks in advance. Please let me know if I'm missing something.

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
1
Best Answer

Answer by Stratosome · Sep 08, 2018 at 05:44 PM

Well, let's look at what (transformPosition - mousePosition) is. transformPosition is the target objects position in 2D and mousePosition is, well, your mouse position. To get the vector from your mouse pointing out to your target, you do like what you're doing: Destination - Origin. So, if we look at a quick little example:

 Mouse ---------> Target
 
 Mouse -------------------------------------------> Target

That arrow is this force vector that you are currently calculating. Obviously as the two get further apart, that arrow is going to get larger looking at the "image". So, if that's the vector you are using for your force, the behavior you describe is expected. If you want it to apply less force the further they get instead, you could try something like this:

 float maxForce = 10.0f;
 float maxDist = 10.0f;
 
 Vector2 forceDirection = (transformPosition - mousePosition).normalized; 
 float distance = Vector2.Distance(transformPosition - mousePosition);
 float forcePercentage = (distance >= maxDistance) ? 0.0f : 1 - (distance / maxDistance);
 
 GetComponent<Rigidbody2D>().AddForceAtPosition(forceDirection * forcePercentage * maxForce, mousePosition); 


I didn't test that code, but I'm pretty sure the idea at least works if not the code hah.

Comment
Add comment · Show 3 · 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 Eno-Khaon · Sep 08, 2018 at 07:29 PM 1
Share

To note: by the original description, it sounds like they were looking for something more like Rigidbody.AddExplosionForce(), which is not available for Rigidbody2D.

With that said, the only other deter$$anonymous$$ing factor is whether rotation is desired, so the difference then would be whether AddForce or AddForceAtPosition is used.

Furthermore, it also might not hurt to save the Rigidbody2D into a variable, so it's not needing to call GetComponent every frame.

avatar image Stratosome Eno-Khaon · Sep 08, 2018 at 08:29 PM 1
Share

Good point about whether AddForceAtPosition is needed or if AddForce would work. I just went along with what he had, but you're right. And yes, saving the Rigidbody2D into a variable would be good.

avatar image andrew196 · Sep 09, 2018 at 10:36 AM 0
Share

thanks a lot. This makes a lot of sense. The confusing part for me was $$anonymous$$us-ing one vector from another

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

95 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Drag Object Script Problem 2 Answers

2D car brakes help??? 1 Answer

Creating a simple swinging vine/rope/line 0 Answers

Rigidbody2D freezes 0 Answers

Unity 2d Physics Collision Jumping / Glitching 0 Answers


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