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 11, 2019 at 08:26 PM · rigidbody2dvelocityaddforce

I need Help with AdForce pls :(

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
1
Best Answer

Answer by Captain_Pineapple · Aug 11, 2019 at 09:01 PM

Hey there,

first off: please no begging in a posts headline... stick to the important informations...


regarding your problem: It's all in the way how you handle velocity in the FixedUpdate...

I will assume that horizontal and vertical are direct values resulting from inputs? If yes then assume a situation where you apply force. This force will be translated to a change in velocity. Then the FixedUpdateoccures and afterwards your object will be moved according to the velocity it has. But every fixed update you set it's velocity to a fix value that is tied to your input values.


So where to start fixing this? choose another way to apply the input to your character movement or change the calculations in your fixed update to actually include the velocity of the last frame. (which will the automatically also include the explosion force)

Let me know if anything was unclear.

Comment
Add comment · Show 3 · 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 11, 2019 at 09:15 PM 0
Share

how do i actually do that? - "change the calculations in your fixed update to actually include the velocity of the last frame. (which will the automatically also include the explosion force)", sry, but never done this b4

avatar image Captain_Pineapple Gamelands-Studio · Aug 12, 2019 at 02:47 PM 0
Share

I put this together:

 if (rb.velocity.magnitude <= speedLimiter)
         {
             rb.velocity = Vector2.Clamp$$anonymous$$agnitude(new Vector2((horizontal * speed), (vertical * speed)), speedLimiter);
         }

works in my head but is not tested... idea is to only fiddle with teh velocity when the magnitude is smaller than max speed. So an explosion should automatically take your control away. As soon as you drop below a certain speed you should be able to move with that maxSpeed again.

And Vector2.Clamp$$anonymous$$agnitude will make sure that your total velocity never exceeds the value of speedLimiter.

Just plug that code into your fixedUpdate and remove your current code there.

avatar image Gamelands-Studio Captain_Pineapple · Aug 12, 2019 at 08:13 PM 0
Share

Thank you So much man your a genius! :'DDDDDDDD

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

112 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

Related Questions

Jumping with Rigidbody.AddForce not working? 1 Answer

Adding force to rigidbody2d to slide 1 Answer

rigidbody2D.AddForce causing other objects to float away 1 Answer

[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer

How can I tweak acceleration and deceleration of a Rigidbody2D with .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