Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ace_Oxide · Jan 12, 2021 at 03:08 PM · physicsaddforce

Horizontal Rigidbody2D.Addforce not working, but vertical works fine?

EDIT! So I loaded up a FRESH PROJECT, created a circle and added my script to it. When I hit start the circle launches off at a 45 degree angle, it works perfectly fine. I also tried adding a horizontal addforce on another object in my original project and it resulted in a jittery teleport to the right.

Something is broken inside my project related to horizontal force... What could it be?!

This has driven me and a bud mad for 24 hours now and I've searched high and low but have not found any similar problems or applicable solutions.

I'm developing a mario-style side scroller game and the concept is simple: Hit the box, a coin pops out with vertical and horizontal force. This is the ONLY code so far in the script attached to the coin.

  void Start()
 {
     forceUp = new Vector2(0, upForce);
     forceSide = new Vector2(sideForce, 0);
     GetComponent<Rigidbody2D>().AddForce(forceUp, ForceMode2D.Impulse);
     GetComponent<Rigidbody2D>().AddForce(forceSide, ForceMode2D.Impulse);
 }

This is the code that instantiates the coin in the box script:

 private void OnCollisionEnter2D(Collision2D collision)
     
 {
     RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 1.0f, layerMask);
     
     if (!isOpen && hit.collider != null)
     {
         isOpened();
         Instantiate(openPrize, new Vector3(transform.position.x, transform.position.y+1.0f, transform.position.z), transform.rotation);
     }
 }



problem is: The coin will pop "up" a distance directly related to the value I plug into the "up force" variable.

The coin will not move horizontally at all. If I jack the horizontal variable up to some stupid number like 5000, it will teleport a short distance to the side.

Comment
Add comment · Show 3
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 MSavioti · Jan 12, 2021 at 03:23 PM 0
Share

$$anonymous$$aybe joining the forces into a single vector helps

  force = new Vector2(sideForce, upForce);
  GetComponent<Rigidbody2D>().AddForce(force , Force$$anonymous$$ode2D.Impulse);
avatar image Ace_Oxide MSavioti · Jan 12, 2021 at 03:26 PM 0
Share

That's how it was initially. I separated the values to try to make it work.

avatar image MSavioti · Jan 13, 2021 at 12:19 AM 0
Share

Try creating a Physics $$anonymous$$aterial 2D, removing all friction and adding it to the object's rigidbody. maybe it's friction behavior.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Jan 12, 2021 at 05:51 PM

It's because you are doing it in 2 steps I think Try

  GetComponent<Rigidbody2D>().AddForce(new Vector2(sideForce, upForce), ForceMode2D.Impulse);

in your Start method, delete the rest.

Comment
Add comment · Show 2 · 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 Ace_Oxide · Jan 12, 2021 at 06:05 PM 0
Share

Nope. I've tried it both ways. It was originally how you describe it.

avatar image logicandchaos Ace_Oxide · Jan 12, 2021 at 08:06 PM 0
Share

I think it's your other script, this line here: Instantiate(openPrize, new Vector3(transform.position.x, transform.position.y+1.0f, transform.position.z), transform.rotation); you instantiate it to position transform.position.x, transform.position.y+1.0f, you could also try setting the velocity instead of AddForce which I find works a lot better for me. O maybe different types of force modes. Also check your object to see if there are restraints set in the inspector.

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

189 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

Related Questions

Get result (force & torque) of AddForceAtPosition? 2 Answers

Update speed and physics makes my rigidbody jiggle 2 Answers

Why is force only being added in the same direction? 1 Answer

AddForce in Update 1 Answer

Advice on keeping distance using AddForce 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