- Home /
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.
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
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...
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.
No need to detect anything at the moment. just place one single gravity source and try to get it working.
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.
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
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
$$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
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!
Your answer
Follow this Question
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