Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Xender · Jan 12, 2014 at 07:37 PM · rigidbodycharactercontrollergravityoverlapsphereaddexplosionforce

How to add explosion force to player?

I trying to creat object that emits gravity fields. I read about it and i think the best way for it is to use addExplosionForce, but i really dont know how to use it.

I have player with rigidbody, characater controlle and my own movement script, and sphere with localGravityScript. This is my code but it don't work:

     void Update () {
         Vector3 location = transform.position;
         Collider[] objectsInRange = Physics.OverlapSphere(location, radius);
 
         foreach (Collider col in objectsInRange) {
             Rigidbody player = col.GetComponent<Rigidbody>();
             if(player != null){
                 Debug.Log("inside sphere");
                 player.AddExplosionForce(4, location, 5);
             }
         }
     }

my target is to create effect of local gravity field, if player enter inside field he should be pulled inside or pushed outside.

Comment
Add comment · Show 13
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 HappyMoo · Jan 12, 2014 at 07:41 PM 0
Share

No, just Use AddForce. Gravity is obviously no explosion.

Also apply your force in FixedUpdate and calculate it based on distance and pretend mass of your sphere.

Formula can be googled

avatar image Xender · Jan 12, 2014 at 10:21 PM 0
Share

still i should use overlapSphere?

avatar image HappyMoo Xender · Jan 12, 2014 at 10:30 PM 0
Share

only if you want to limit from how far you can be influenced. I'd leave that out now and only add it back later for optimization if you have a lot of planets far away...

avatar image Xender · Jan 12, 2014 at 11:01 PM 0
Share

so how i should detect, player is or not in gravity influance range?

$$anonymous$$y idea is to create game with FPP camera with player can place gravity points and use it to solve riddles.

avatar image HappyMoo Xender · Jan 12, 2014 at 11:16 PM 0
Share

No need to detect anything at the moment. just place one single gravity source and try to get it working.

avatar image Xender Xender · Jan 14, 2014 at 04:38 PM 0
Share

In my way to creat gravity source i found one problem. I can't add force to player (object with Character Controller, my own movement script and rigidbody). Gravity works perfectly with simple rigidbody object but with player wont and i don't know why.

avatar image HappyMoo Xender · Jan 14, 2014 at 05:35 PM 0
Share

You can add force to any rigidBody....

Try, if you get an error message, try to figure out what it says.

Post code and error if you don't get it

Show more comments
avatar image HappyMoo · Jan 14, 2014 at 10:31 PM 0
Share

Sorry, no offense, but this is obviously way over you skill level and I don't want to write the code for you... I would recommend to start with something easier.

There's no trigger involved. You have gravity sources. You need to calculate the strength based on body mass of the player and the pretend mass of the planets. Also the Force should be added in the direction of the planet.

Good luck

avatar image Xender · Jan 14, 2014 at 11:24 PM 0
Share

$$anonymous$$ath calculations is not problem for me, the problem is that the AddForce() method do nothing. No matter what i put inside it won't work. But... To move character i use CharacterController.$$anonymous$$ove() $$anonymous$$ethode, and when i disable this methode, i can't move my character but AddForce() works and push my character. So the question is: can i use both, rigidbody.AddForce() and CharacterController.$$anonymous$$ove() on the same character

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by OP_toss · Jan 14, 2014 at 11:43 PM

The problem is your rigid body is kinematic, aka animated. So in order to have your rigid body be active, you need to disable kinematic. This, however, will not allow you to control your character. So now you realize why we can't always use rigid bodies for player characters. BACK TO THE DRAWING BOARD!

So instead of that, just calculate it yourself and apply it to your move vector before using CharacterController.Move. Something like this:

 Vector3 velocity;
 
 void FixedUpdate()
 {
   UpdateInput();
   UpdatePhysics();
 
   controller.Move( velocity );
 }
 
 ...
 
 void UpdatePhysics()
 {
   Vector3 gravity = Vector3.zero;
   foreach (Planet p in planets)
   {
     //todo, calculate distance from me to planet
     //based on distance, add to gravity in direction of planet
     //add gravity vector to velocity
   }
 }

So essentially, create your own physics. Pretty simple with things like gravity and basic input. Gets more complicated as you go, but gives you total control of your movement vector for each update. Best solution I've found. Hope this helps!

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

20 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

Related Questions

Gravity not working 1 Answer

FPS Character Controller with the push DOWN rigidbodies ability 0 Answers

3rd person Character Control with kinect 0 Answers

Character Controller Object Randomly Falls Through Colliders 3 Answers

How can I slow Gravity 2 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