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 Dyonalex · Oct 14, 2016 at 05:12 PM · unity 5rigidbody2dforcerigidbody.addforce

Trying to launch a rigidbody towards mouseposition but nothing makes it work.

I'm making a 2d platformer where you can launch yourself towards the place where the player clicks. I'm new to unity and none of the answered questions here seem to solve my problem. I'm using a rigidbody and the part of the script that won't work is as follows: void FixedUpdate () { float move = Input.GetAxis("Horizontal");

     body.velocity = new Vector2(move * maxSpeed, body.velocity.y); //basic player movement

     if (Input.GetMouseButtonDown(0)){
         mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         direction = (mousePos - body.position);

      }
     if (Input.GetMouseButtonUp(0))
     {
         direction.Normalize();
         body.AddForce(direction * jumpForce);
     }   

For some reason the AddForce function does nothing to the player and I'm just clueless at the moment. Using other people's code won't work either because I've tried A LOT.

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 Naphier · Oct 14, 2016 at 05:17 PM

Have you debugged the code? What is the values for direction? What is the value of jump force? Also, try ForceMode.Impulse as the second parameter of AddForce.

EDIT: Second issue was that the script above is modifying the rigidbody's velocity directly. This will cause the physics engine to require a recalculation and skip a few frames and can result in bad behavior. Rigidbodies should be moved by AddForce or one of the methods in the Rigidbody class.

Comment
Add comment · Show 6 · 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 Dyonalex · Oct 14, 2016 at 05:33 PM 0
Share

Force$$anonymous$$ode.Impulse didn't seem to do anything and I have debugged the code. I noticed that when I set my jumpforce to 1000 my character jumps to the right and then up very fast no matter where I click. $$anonymous$$y direction values seem to be around (0.90, 0.30) most of the time.

avatar image Naphier Dyonalex · Oct 14, 2016 at 05:38 PM 0
Share

In 99% of the cases, Force$$anonymous$$ode.Impulse is what you want to use with any jump-like motion. Otherwise you're over-applying force. Your mouse position needs to be converted from screen coordinates to world coordinates and it does need a z component for that. This answer will help with that http://answers.unity3d.com/questions/540888/converting-mouse-position-to-world-stationary-came.html

avatar image Dyonalex Naphier · Oct 14, 2016 at 06:28 PM 0
Share

Alright thanks a lot that did help and I understand why it does, the only issue I have now is that it doesn't want to move the rigidbody over the X-axis properly. It goes up and down very smoothly but when I want it to move left or right it just teleports to the left or right depending on my jump force.

Show more comments
Show more comments
avatar image Naphier · Oct 15, 2016 at 03:13 AM 0
Share

@Dyonalex - yeah, Unity tutorials unfortunately don't always use the correct methods or are outdated and the API has changed. It's a good idea to make a habit of looking up stuff in the documentation. Please accept my connect as the answer. Thanks

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RigidBody2D projectile to target calculation 1 Answer

How to move Rigidbody2D on Player a certain amount at a certain speed, for a dash move and knockback? 2 Answers

Move an object via Rigidbody2D Physics and Transform at same time 0 Answers

Unity 2D addforce problem 1 Answer

Rigidbody2D Force Problem 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