- Home /
Detect explosion Character Controller
I have a character controller and a missile that causes an explosion. I want the explosion to knock back/allow the player to rocket jump. How can I do this? The Character Controller does not respond to rigidbody physics such as a Detonator explosion or a high velocity rigidbody projectile. What is the best way to allow the functions of both on my Character Controller so that it is effected by explosions but still functions like a Character Controller?
Do you have Rigidbodies and Colliders on both objects ?
Well, one is an explosion, so no, but it's able to push rigidbodies away. Also, character controllers with rigidbody's don't act like a rigidbody.
I'm not 100% sure on this but character controller adds a rigidbody underneath but it acts weird with any physics you apply to it.
add a rigidbody but disable the rigidbody.
on collision with an explosion disable character controller and enable rigidbody. Add forces to rigidbody to simulate explosion, on landing disable rigidbody enable character controller.
Answer by sparkzbarca · Oct 30, 2012 at 06:04 PM
well do you understand basic coding?
Like do you know what an array is?
Sphere cast all returns an array
//impact point you'll need to find, its the point at
//which the round exploded
vector3 ImpactPoint;
//DamageRadius is just chosen by you based on
//what you think is appropriate explosion radius
float DamageRadius;
//this is an array, it will contain information
//on every object that was in the explosion radius
RayCastHit[] ArrayOfObjectsHit;
//this is the line that finds all objects with
//colliders inside the radius DamageRadius whose
//center or origin is ImpactPoint
//this Returns an array which we'll store.
ArrayofObjectsHit = physics.spherecastall(ImpactPoint,DamageRadius)
//This will do alot of things so read carefully
//it will go through the array of objects, it will take the players
//position and the explosions position and use that to figure out which
//way to throw the player.
//it will then move the player some
vector3 TossDirection;
//speed is meters per second, thats how unity does speed for a character controller
//speed is how fast you will be thrown
float speed = 1;
foreach (RayCastHit ObjectHit in ArrayOfObjectsHit)
{
if (ObjectHit.transform.GameObject.Tag == "Player")
{
TossDirection = ObjectHit.transform.position - ImpactPoint;
//this next line varies, its the line that will move the player
//but physics based movement is done differently than
//character controller movement which is done slightly
//differently than transform movement or kinematic
//rigidbody transform movement
//this assumes your using a character controller
//we will multiply the direction by the speed to get a Movement Vector
ObjectHit.transform.GameObject.CharacterController.Move(TossDirection * Speed);
}
}
That took awhile, mark as answered. If you can't turn that into the answer you need to learn more about programming.
Yes, thank you. I just didn't understand how the array was outputted.
Answer by gnubberlang · Oct 29, 2012 at 11:17 PM
Looks like you need to do away with the character controller peice all together and work with a rigid body + some sort of collider.
I was dealing with this issue too. I got it working with a box collider and rigid body only.
http://www.packtpub.com/article/unity-3.x-rigidbody-character-controller-script
Yes, the point of the CharacterController is that it isn't affected by external forces. If you want this, use a Rigidbody.
Answer by sparkzbarca · Oct 29, 2012 at 10:04 PM
add a rigidbody but disable the rigidbody.
on collision with an explosion disable character controller and enable rigidbody. Add forces to rigidbody to simulate explosion, on landing disable rigidbody enable character controller.
note, this is how games do ragdoll physics on characters.
If you dont want physics do a rough simulation by using charactercontroller.move() to move the character in the direction opposite the explosion at the speed thats the max speed / distance. The direction opposite can be found by
explosion.position - character.position.
That willl give you a vector pointing from explosion in the direction of character.
at the point of the "explosion" you spherecastall to find all nearby colliders
http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCastAll.html
SphereCastAll basically returns all colliders within the radius you define. It will return an array containing all colliders it hit. Tell it to ignore terrain. You now know what was within the "blast" radius.
So basically you attach a spherecastall script to the projectile and have it called on explosion.
you then check all the colliders returned and if it returns a player you damage them based on distance and move them based on distance in a direction opposite the explosion.
Thanks for the help! How would I get and use the results of my SphereCastAll?
Answer by ZenithCode · Oct 29, 2012 at 10:08 PM
Ok I think you have to 'hack this'. What I would try is to: ` 1. Add an empty Game object as child of my character 2. Add a box collider and set it to trigger and add a rigidbody (disable this) 3. Put a script in there which handles OnTriggerEnter 4. In this method, that is on trigger, disable the character controller on the parent and enable the character controller. `
Please let me know if this helps.
Your answer
Follow this Question
Related Questions
FPS Controller and Explosion? 2 Answers
How to trigger different prefabs (JavaScript) 2 Answers
Shuriken Explosion Example - Collision 0 Answers
Move a detonator 1 Answer
Object only explodes when it collides with certain object, not all rigidbodies. 2 Answers