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 /
avatar image
0
Question by bhafenri · Aug 29, 2019 at 06:18 AM · rigidbodyrigidbody2dphysics2daddforce

Unity 2D Physics .AddForce

Hey Everyone,

So I am trying to implement knockback functionality for my enemies. When the enemy stands still, the knockback works correctly, but when its moving towards the player, .AddForce seems to be overwritten with my movement code, even though it doesn't get executed. Both entities have dynamic rigidbodies with isKinematic set initially to false.

Intended Functionality: alt text Current Functionality when Entity Moves: alt text

I know I'm missing something with the physics, but I cannot see what it is. Any help would be greatly appreciated. Here is my logic below:

     internal void Hurt (GameObject Enemy) {
         var EnemyEntity = Enemy.GetComponent<Entity>();
         EnemyEntity.Health -= 10;
         Rigidbody2D EnemyBody = Enemy.GetComponent<Rigidbody2D>();
         PreviousBodyType = EnemyBody.bodyType;
         WasKinematic = EnemyBody.isKinematic;
         EnemyBody.bodyType = RigidbodyType2D.Dynamic;
         EnemyBody.isKinematic = false;
         Vector2 Difference = (Enemy.transform.position - transform.position);
         Difference = Difference.normalized * GlobalVariables.Instance.KnockbackIntensity;
         if (PreviousBodyType != RigidbodyType2D.Static) {
             EnemyBody.AddForce(Difference, ForceMode2D.Impulse);
         }
         if (EnemyEntity.Health <= 0) {
             EnemyBody.isKinematic = true;
              StartCoroutine(EnemyEntity.Die());
         }else {
             EnemyEntity.IsHurting = true;
             StartCoroutine(HurtRecovery(Enemy));
         }
     }
 
     private IEnumerator HurtRecovery (GameObject Enemy) {
         if (Enemy != null) {
             var Sprite = Enemy.GetComponent<SpriteRenderer>();
             var Entity = Enemy.GetComponent<Entity>();
             var Body = Enemy.GetComponent<Rigidbody2D>();
             var EnemySpriteColor = Sprite.color;
             Sprite.color = Color.red;
             yield return new WaitForSeconds(GlobalVariables.Instance.KnockbackTime);
             Sprite.color = Entity.BaseColor;
             Body.velocity = Vector2.zero;
             Body.isKinematic = WasKinematic;
             Body.bodyType = PreviousBodyType;
             Entity.IsHurting = false;
         }
     }

Along with my movement script:

 public void Move(Vector2 Movement) {
         if (IsDead() || IsHurting) {
             return;
         }
         if (IsFrozen == true) {
             Body.velocity = new Vector2(0, 0);
             Animator.SetBool("IsMoving", false);
             return;
         }
 
         if (Movement.x == 0 && Movement.y == 0) 
         {
             Body.velocity = new Vector2(0, 0);
             Body.isKinematic = true;
             Animator.SetBool("IsMoving", false);
             return;
         }
         else
         {
             Body.isKinematic = false;
             Body.MovePosition(Body.position + Movement * Speed * Time.deltaTime);
             Animator.SetBool("IsMoving", true);
         }
 
 
         if (Movement.x != 0 && Mathf.Abs(Movement.x) > Mathf.Abs(Movement.y))
         {
             
             if (Movement.x > 0)
             {
                 Animator.SetInteger("HorizontalMove", 1);
                 if (IsSimpleMovement == true) {
                     Sprite.flipX = true;
                 }
             }
             else if(Movement.x < 0)
             {
                 Animator.SetInteger("HorizontalMove", -1);
                 if (IsSimpleMovement == true) {
                     Sprite.flipX = false;
                 }
             }
             Animator.SetInteger("VerticalMove", 0);
         }
         else
         {
             Animator.SetInteger("HorizontalMove", 0);
             if (Movement.y != 0)
             {
                 if (Movement.y > 0)
                 {
                     Animator.SetInteger("VerticalMove", 1);
                 }
                 else if (Movement.y < 0)
                 {
                     Animator.SetInteger("VerticalMove", -1);
                 }
             }
             else
             {
                 Animator.SetInteger("VerticalMove", 0);
             }
         }
     }

spider-still.gif (352.8 kB)
spider-moving.gif (389.2 kB)
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 xxmariofer · Aug 29, 2019 at 07:28 AM

Try setting the velocity to 0 just before you add the force

  if (PreviousBodyType != RigidbodyType2D.Static) {
 
              EnemyBody.velocity = Vector2.zero;
              EnemyBody.AddForce(Difference, ForceMode2D.Impulse);
  }
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 bhafenri · Aug 30, 2019 at 08:41 AM 0
Share

Unfortunately it did not work, still had the same results :/ Perhaps, the $$anonymous$$ove() is getting called somewhere it shouldn't be. When I debugged it, the breakpoint wasn't being hit. But, I'll do some more testing tonight. Thanks for your help @xxmariofer

avatar image xxmariofer bhafenri · Aug 30, 2019 at 09:45 AM 0
Share

that was my first though, but at the very start of the method you have an if ishurt that should never get passed

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

152 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

Related Questions

Child GameObject can't use MovePosition on parent's Rigidbody2D? 3 Answers

How to find speed in certain direction? 1 Answer

Inconsistent gravity/random forces/solver problem? (forum crosspost) 0 Answers

increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer

Stack of RigidBody2D, Freeze X and Z, Odd Behavior 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