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

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

Platformer2D Player sticks to platform corners 0 Answers

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

Adding force at an angle 2D,Adding force at an angle 1 Answer

move 2d character affected by physics with velocity and/or add force 2 Answers

Jump Height Different On Android 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