• 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 tylerb115 · Dec 08, 2015 at 11:56 AM · physicsrigidbody2dphysics2daddforce

Rigidbody2d.addforce in the direction of mouse problem

I'm currently using this code to addforce in the direction of the mouse:

 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 mouseDir = mousePos - gameObject.transform.position;
 mouseDir = mouseDir.normalized;
 
         if (Input.GetMouseButtonDown(0)) {
             rb.AddForce(mouseDir * 1500);
         }

It works fine however, the problem is that the force applied changes depending on how far my mouse is from the object. Is there any other way to do it so that the force is not affected by the distance from the mouse?

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 MelvMay · Dec 08, 2015 at 12:10 PM

First, I would ensure that you're not doing this in anything other than the FixedUpdate callback otherwise it'll be dependent upon the frame-rate. Second, you're using Vector3 and not Vector2 which Unity is implicitly converting for you however in this case, it's causing a problem as you're getting Z in your 'mouseDir'.

I would reset the Z like so:

 void FixedUpdate ()
 {
     var mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     var mouseDir = mousePos - gameObject.transform.position;
     mouseDir.z = 0.0f;
     mouseDir = mouseDir.normalized;
 
     if (Input.GetMouseButtonDown(0))
     {
          rb.AddForce(mouseDir * 1);
     }
 }

... before normalizing to remove any Z difference (assuming you're not using a 3D mouse).

With this change, it will always produce a unit-normal in the direction of the mouse as you desire and ignore any Z differences. In the very least, it'll remove Z from the normalized components. If you're seeing anything else then something else is going wrong.

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 wibble82 · Dec 08, 2015 at 12:13 PM 0
Share

Hey - this is basically correct. The only dodgyness would be if you are using a perspective camera, not an orthographic one. If that's the case then you'll need to do a little more work to get a good mousePos - simply clearing z won't cut it.

avatar image tylerb115 · Dec 08, 2015 at 03:53 PM 0
Share

Thanks for the reply! Unfortunately, the amount of force added still changes on how far the mouse is from object being thrown. This is the full script I'm using:

 bool used = false;
 
     public Rigidbody2D rb;
     Collider2D cl;
 
     void Start () {
         rb = GetComponent<Rigidbody2D>();
         cl = GetComponent<Collider2D>();
 
         rb.gravityScale = 0;
         cl.shared$$anonymous$$aterial.bounciness = 0f;
     }
 
     void FixedUpdate () {
         var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         var mouseDir = mousePos - gameObject.transform.position;
         mousePos.z = 0.0f;
         mouseDir = mouseDir.normalized;
         
         if (Input.Get$$anonymous$$ouseButtonDown(0) && used == false) {
             cl.shared$$anonymous$$aterial.bounciness = .7f;
             rb.gravityScale = 1;
             
             rb.AddForce(mouseDir * 1500);
 
             used = true;
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R)) {
             Application.LoadLevel (Application.loadedLevel);
         }
     }

Any problems you see with it?

avatar image wibble82 tylerb115 · Dec 08, 2015 at 03:58 PM 0
Share

You've written:

 mousePos.z = 0.0f;

That actually has no effect at all, as you don't use mousePos after using it to calculate mouseDir.

Replace it with mouseDir.z = 0.0f as in the example, and it should work fine.

avatar image tylerb115 · Dec 09, 2015 at 09:02 AM 0
Share

Oh jeez, I didn't even realize that $$anonymous$$elv$$anonymous$$ay said that. Thanks for pointing it out. Thanks so much guys!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The character velocity changes while the rigidbody is kinematic 1 Answer

how to make my character stay stable after hit? 2 Answers

Workarounds for Higher Control When Using Physics2D? 0 Answers

Is it possible to disable collisions for separate physics scene? 0 Answers

Stack of RigidBody2D, Freeze X and Z, Odd Behavior 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