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 /
  • Help Room /
avatar image
0
Question by sunnykwong91500 · Mar 24, 2018 at 04:28 AM · rigidbody2dvelocityaddforceteleporthorizontal

RigidBody2D.AddForce() horizontally is not working!

Hello all, I am having trouble with AddForce horizontally, for some reason my player would be teleporting a set distance instead of actually being "pushed." I have searched the forums and have tried out multiple solutions but none of them work. I am currently making a 2D game, and the method I am working on is when the player takes damage, it will get knocked back.

 void takeDamage(float value)
     {
         health -= value;
         Vector2 move = new Vector2(rb2D.velocity.x + 20f,rb2D.velocity.y + 2f);
         rb2D.AddForce(move*2f, ForceMode2D.Impulse);
     }

I have tried changing the ForceMode2D to both Impulse and Force but it does not work. I've also tried changing the velocity of the player instead of adding force but it also does not work. The strange thing is the vertical portion of the knockback works, it will get pushed up, but teleport sideways. It works like this when I add velocity and add force. If anyone could help I would gladly appreciate it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by sunnykwong91500 · Mar 25, 2018 at 12:24 AM

Ok so the problem I found was that I kept on setting the velocity of my player to 0 in my moveHorizontal() method. Since this is called in fixedUpdate() it occurs repetitively by setting the float move to 0 and then setting to velocity to 0. So for anyone who has any trouble with this, cause I've seen a lot of posts about this, make sure you are not setting the velocity of the player to 0 at any other point.

Comment
Add comment · Show 1 · 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 exzizt · Mar 25, 2018 at 12:32 AM 0
Share

"Where else in your code do you move your player horizontally? The issue is probably caused somewhere else."

Gee, not like my answer led you to solve it. Lol

avatar image
0

Answer by exzizt · Mar 24, 2018 at 04:31 AM

You say that vertically works great, but horizontally is "teleporting". Maybe this is because for the vertical you are adding 2, but for the horizontal, you are adding 20. Is this a typo? Try 2 instead.

Comment
Add comment · Show 13 · 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 sunnykwong91500 · Mar 24, 2018 at 04:35 AM 0
Share

I've tried multiple different values, all it does is just increase the distance that it teleports.

avatar image exzizt sunnykwong91500 · Mar 24, 2018 at 04:40 AM 0
Share

Try move.normalized * 2f ins$$anonymous$$d.

avatar image sunnykwong91500 exzizt · Mar 24, 2018 at 04:42 AM 0
Share

move.normalized *2f made it so that the player barely moved at all. That removed the vertical portion and the horizontal movement moved only a tiny bit.

Show more comments

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

130 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

Related Questions

Trying to add force to an object... is this done correctly? 0 Answers

RigidBody2D velocity randomly drops to zero 1 Answer

Terminal velocity on x and y axis is higher than normal when force is applied to both axis simultaneously. 0 Answers

Using rb.velocity and Add Force on the same gameobject? 0 Answers

How do I ignore gravity when using Rigidbody and always have the same jump force? 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