• 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 Ocamper21 · Sep 28, 2021 at 10:30 PM · physicsrigidbodyforce

Rigidbody2D.AddForce not working as expected

Alright so I'm not the brightest when it comes to physics stuff and generally Unity so forgive me if this is not the best question (I looked through the forums and people seemed to have similar questions but nothing really helped me out) but as of now my Rigidbody2D (Dynamic, Mass = 1, Gravity Scale = 0) is not functioning properly with an AddForce function. My script is:

 void Update()
 {
     movement.x = Input.GetAxis("Horizontal");
     movement.y = Input.GetAxis("Vertical"); //Store direction input values as the Player's Vector2 Axis

     mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition); //mousePos stores the mouse position within itself. However, the mousePosition function works using pixels, instead of units which is what Unity usually uses inside of it. So there has to be a reference to the camera which converts pixels to world units

     if (Input.GetKeyDown("space") && !rollState)
         rollState = true;
 }

 void FixedUpdate()
 {
     rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime); //Constantly moves the Player to its current position PLUS the directional input values from the Vector2 variable, multiplied by the set speed and by the fixedDeltaTime to avoid framerate discrepancies between machines

     Vector2 lookDir = mousePos - rb.position; //Vector2 which sets itself to the substraction of the mouse position and the player's position to figure out where the player should point. This doesn't return an angle, though.
    
     float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg; //Atan2 imagines the player at 0, 0 on a plane and it imagines a line between this point and the mouse position. The now created angle between the plane and the line will now dictate where the player should aim at. Then Rad2Deg because reasons. I don't even understand what im doing anymore nor do i understand this function oh my god im just stealing code from brackeys

     rb.rotation = angle;

     if (rollState)
     {
         Debug.Log("start of addforce");

         // PROBLEM HERE
         rb.AddForce (movement * 200, ForceMode2D.Impulse);
         // PROBLEM HERE

         Debug.Log("end of addforce");
         rollState = false;
     }
 }

}

I am using a MovePosition for normal movement as well so I am not quite sure if that's messing it up. The Debug.Logs are showing up as expected so the issue isn't there by the way. I tried messing with different values and a different Vector2 for the AddForce but nothing seemed to work. Appreciate any help!

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

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Problems making a glider 3 Answers

Character Controller meets Rigidbody 1 Answer

Calculate velocity to set to reach a target point on a plane considering the drag. 0 Answers

Touch controlled ice puck 0 Answers

Add Relative Force 2 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