Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by tbngrd · Nov 03, 2018 at 12:18 AM · rigidbody2daddforceknockbackforcemode

How do I get ForceMode2D.Impulse to work?

Hi everyone!


I'm trying to apply an Impulse force to my Player Character to knock him back when he's being hit by an enemy. I'm doing this inside a separate function inside my player controller that gets the direction for the knockback, as well as the amount of knockback from whatever object the player is attacked.


Now, if I don't specify the ForceMode2D, it works fine. Albeit only when the float knockback is a ridiculously high number, like 10'000. This makes sense however, as in it's normal mode, AddForce is applied over time, and as it's only executed once it needs that high number. That however isn't smooth at all, and as I understand it, that is where ForceMode2D.Impulse should come into play.

As soon as I set the ForceMode2D to Impulse however, it doesn't work at all anymore.

Here's the simple function:


     public void knockBack(Vector2 knockbackDirection, float knockback)
     {
             rb2d.AddForce(knockbackDirection * knockback * Time.deltaTime, ForceMode2D.Impulse);
     }



Like this it doesn't move at all. Seemingly no force is being applied to the Rigidbody. As soon as I remove ForceMode2D.Impulse it works again as mentionned. The Vector 2 knockbackDirection and float knockback have correct values in them.


The Rigidbody2D of the object is set to dynamic, with a mass of 1.

What am I missing?

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
1

Answer by Sazails · Nov 03, 2018 at 03:28 PM

You really should tell us the knockbackDirection you are doing and also make sure not to use Time.deltaTime as it is not needed. Time.deltaTime should be used in functions such as Update() and FixedUpdate(). Also make sure your rigidbody2D doesn't have any constraints on rotation or position.

To see impulse working , I'd prefer to turn gravity to 0 and then test it. By going to Edit > Project Settings > Physics 2D and change Gravity from -9.81 on the y to 0.

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 tbngrd · Nov 22, 2018 at 12:23 PM 0
Share

Hi @Armanis2017 ! Apologies for getting back to you so late - thanks a lot for your reply. I figured out the issue shortly after posting myself, and forget to post in on here.

The issue and the fix were:

In the FixedUpdate of the script, the part that handles player movement, the velocity was constantly being set. The AddForce Impulse was overwritten by whatever velocity that was being input in the movement part (in case the player wasn‘t moving - zero). I solved it by implementing a simple timer for the length of the knockback. That now allows me to control the knockback smoothly by setting the length and knockback amount.

avatar image
0

Answer by ProjektSpark · Nov 03, 2018 at 03:22 PM

Damn, this is killing me too. Keeping an eye out here if someon ehas a solution !,Damn this is killing me too. Keeping an eye out here if someone has a solution !

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 tbngrd · Nov 22, 2018 at 12:25 PM 0
Share

Hi @ProjektSpark ! In case you're still having this issue - I identified the problem in the meantime and was able fix it as follows:

In the FixedUpdate of the script, the part that handles player movement, the velocity was constantly being set. The AddForce Impulse was overwritten by whatever velocity that was being input in the movement part (in case the player wasn‘t moving - zero). I solved it by implementing a simple timer for the length of the knockback. That now allows me to control the knockback smoothly by setting the length and knockback amount.

Let me know if you need more help, then I could try to explain further or post my script.

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

98 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

Related Questions

issue with the rigid body x axis ,addforce doesn't work as it should for x axis 1 Answer

AddForce knockback x axis according to mouse.position more then y axis (2D game) 0 Answers

Deteriorating force on object? 0 Answers

Using direction and Rigidbody2D.AddForce() to move towards object. 1 Answer

AddForce to x axis when mouse position is close to x axis ? And if closer to x axis the more addforce 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