• 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 badi-haki · Jun 30, 2016 at 02:28 PM · c#rigidbody2daddforceknockbackforcemode

Why doesn't my knockback function apply force to it's target?

I'm making a 2-D game, and thought I'd try my hand at a simple knockback script using ForceMode2D and AddForce. The problem is, when my character hit his target, most of the time the force isn't applied, and if it is applied, it will be on the second or third hit in my combo string. Here is my code: public int dmg = 0; public float knockForce = 0f;

     void OnTriggerEnter2D(Collider2D target) // Collider2D
     {
         if(target.tag == "Player") //may have to be other.tag != "Player") if it doesnt work
         {
             // knockback
             Vector2 directionVector = target.transform.position - transform.position;
             directionVector.Normalize();
             Rigidbody2D targetPlayerBody = target.GetComponentInParent<Rigidbody2D>();
             ForceMode2D mode = ForceMode2D.Impulse;
             targetPlayerBody.AddForce(directionVector * knockForce, mode);
             Debug.Log(directionVector);
 
             //damage
             Debug.Log("Hit!!");
             charHealth targetPlayerHealth = target.gameObject.GetComponentInParent<charHealth>();
             targetPlayerHealth.TakeDamage(dmg);

I've set up my animations in mecanim with animation events to control when damage and knockback force is applied, and how much is sent through. I know that the damage is being sent, so I figured knockForce was being sent through as well. I've now confirmed through the editor that the force is being applied to the attack's properties, but it's still having next to no effect on it's target.

I'm kinda, sorta really new to this, and although I can code a little C#, I'm obviously just a beginner, so any and all help is greatly appreciated.

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
0

Answer by KBEK · Jun 30, 2016 at 06:06 PM

It looks like you set knockForce = 0f at the beginning of your code. This would make it so no force is getting added.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Knockback player on collision 2 Answers

AddForce rigidbody 2d not moving object 2 Answers

RigidBody2D velocity randomly drops to zero 1 Answer

2D Knockback problem 2 Answers

Using rb.velocity and Add Force on the same gameobject? 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