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 Gamelands-Studio · Aug 12, 2019 at 11:49 AM · 2drigidbody2daddforce

how to add multiple forces on a single object

my player uses rigidbody.velocity to move, and also have an alien that explodes, and also has an explosion effect that pushes objects away, look here what the problem is: https://drive.google.com/open?id=1_-mfqw9JTDO01FMklGxCtup5ZQoGu-4R as you see in the video, all mobs flew away, only the player because i'm moving him using rigidbody, i tried to disable my controller script, and it worked, anyone help pls, here's my movement piece of code:

      void FixedUpdate()
      {
          if (horizontal != 0 && vertical != 0)
          {
              rb.velocity = new Vector2((horizontal * speed) * speedLimiter, (vertical * speed) * speedLimiter);
          }
          else
          {
              rb.velocity = new Vector2(horizontal * speed, vertical * speed);
          }
      }

and here is the explosion piece of code:

 public override void FixedUpdate() { 
 base.FixedUpdate();
  checkDistance(); 
 } 
 void checkDistance() { 
 if (player != null) { 
 if (Vector2.Distance(transform.position, player.position) <= 1.75f) 
 {
 
                  Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, radius);
  
                  foreach(Collider2D collider in colliders)
                  {
                      Rigidbody2D rb = collider.GetComponent<Rigidbody2D>();
                      if(rb != null)
                      {
                          AddExplosionForce(rb, explosionForce, transform.position, radius);
                      }
                  }
  
                  GameObject instance = SpawnObject.WithRotation(WormSplashEffect, transform);
                  Destroy(instance, 1f);
                  Destroy(gameObject);
              }
          }
      }
  
      public void AddExplosionForce(Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius)
      {
          var dir = (body.transform.position - explosionPosition);
          float wearoff = 1 - (dir.magnitude / explosionRadius);
          body.AddForce(dir.normalized * explosionForce * wearoff);
      }
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 DenisGLabrecque · Aug 12, 2019 at 12:06 PM

I think you don't want your player character to move when the explosion force gets applied? Then why not check whether the rigidbody is part of your player, and ignore that one in your foreach loop? Not sure what you mean by "I removed the controller script and it works". What works?

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 Gamelands-Studio · Aug 12, 2019 at 12:15 PM 0
Share

no you did not understand what i mean, as you see in the video, the mobs flew away smoothly, but the player only a bit and unsmoothly, that is because i'm using the player's rigidbody to move him, i want the explosion to affect the player same as it affects the other mobs, is there any way to add forces?

avatar image DenisGLabrecque Gamelands-Studio · Aug 12, 2019 at 10:23 PM 0
Share

Have you tried offsetting the explosion force away from the character's rigidbody? I have a suspicion the explosion force doesn't have a direction to act, because it is on position 0,0.

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

215 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Rigidbody2d addforce isn't moving character 1 Answer

Shoot logic, bullet instantiation 1 Answer

Need help on my script to calculate gravity for a mass 1 Answer

Movement stops working randomly 1 Answer

Rigidbody2D.velocity.x not working but Y does 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