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 Derptastic · Mar 10, 2017 at 02:35 PM · rigidbody2daddforcecollision2d

AddForce not pushing back my RigidBody2D

I am trying to add force in the opposite direction of my colliding object (in this case the player). I've scrolled through a dozen of answers but they seem to give me no results. AddForce simply refuses to move the object. (2D space)
This is how I move my enemy which I want to push back on collision. To clarify the object is Dynamic, Mass is 1 and gravity scale is 0:

  public void Movement_Follow(GameObject player, float Speed)
     {
         Vector3 playerPosition = player.transform.position;
         transform.position = Vector3.MoveTowards(transform.position, playerPosition, Speed);
     }

However, even with the moving commented out and simply spawning them at a certain position and then running into them still gives me nothing.

And this is how I try to push it back when it bumps in the player (opposite direction):

 private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.name == "Player")
         {
             health.Damage(5);
             float force = 50;
             //rb is the components RigidBody2D initialized in Start()
             rb.AddForce(-collision.gameObject.GetComponent<Rigidbody2D>().velocity.normalized * force, ForceMode2D.Impulse);
         }
     }

I considered that maybe I don't have enough force applied, but even values of 500 000 produce no effect. Different methods of adding force also give me no results.

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
2

Answer by MelvMay · Mar 10, 2017 at 03:19 PM

You added a Rigidbody2D because you want it to update the Transform position and rotation. This is what it does. You then proceed to directly modify the Transform position yourself overwriting what the Rigidbody2D is doing.

Do not modify the Transform if you're using a Rigidbody2D.

If you want to move the body or change its rotation using Rigidbody2D.MovePosition or Rigidbody2D.MoveRotation. These move the Rigidbody2D to the specified position/rotation during the next fixed-update. If you set your Rigidbody2D to use interpolation then the Transform will be updated per-frame giving you smooth movement as well.

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 Derptastic · Mar 11, 2017 at 12:01 PM 0
Share

That solved some other issues I had on the movement, but still, the object does not react when AddForce is applied to it. Not even a slight bump backwards. This is how the movement looks like now:

   velocity = (player.transform.position - transform.position).normalized;
      public void $$anonymous$$ovement_Follow(Rigidbody2D body, Vector2 velocity, float speed)
         {
             body.$$anonymous$$ovePosition(body.position + velocity * speed * Time.fixedDeltaTime);
         }

The collision is still the same.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Add force once when object is created 1 Answer

Rigidbody2D.AddForce fails to add force 0 Answers

Jump problem 1 Answer

Unity 2D Physics .AddForce 1 Answer

Can someone help me with this Rigidbody2D bug? 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