Can I assign a collider to an explosion? If not, how can i detect if the player is inside an explosion radius?
Hi there happy Unity fellows :)
I think the question title actually covers it all pretty well.
Im making a driving game, there are enemies which spawn on the side of road and when my player car gets close, they fire a rocket in front of the player. I need to detect if the car is within a certain radius of the explosion centre. So when I can detect that I'm assuming I can then use rb.AddExplosiveForce to make the car fly a bit or something :)
But I cannot figure out how is the recommended way to get the blast radius collider (or equivilent)
Thanks for any help as usual
Answer by Jawchewa · Jul 22, 2017 at 02:38 AM
You could probably make this work with a collider, but if you're just checking within a radius, you could also consider checking the distance between the two GameObjects. That would look something like this:
void Update ()
{
GameObject car;
GameObject explosion;
float radius = 10;
if(Vector3.Distance(car.transform.position, explosion.transform.position) < radius)
{
}
}
Although if your really want to do it using a collider, you can just add a sphere collider to the gameObject with the explosion, and add a script similar to this to the car:
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Explosion")
{
//Add Force
}
}
Thank you for this. Unfortunately I'd already had help from someone else before I read this. But they gave me a way that manually faound the Vector3.Distance. So I didnt even know that existed until i just read your answer. Ah well, better learning it late than never i suppose :) Cheers
Answer by 123neri123 · Jul 22, 2017 at 12:16 AM
Hey, you need to make with the explosion a collider that can detect the players, objects and etc there is a good video for you, watch it Grenades and Explosions in Unity 3d
thank you. I ended up using a method someone taught me which involved sqr$$anonymous$$agnitude to find the distance of my player vector, to the explosion centre. $$anonymous$$y game is very simple so only the players car is being affected by the explosions. I will definitely take a look at the video soon though as need to know how to make the explosion a collider for bigger, better projects to come :)
Your answer
Follow this Question
Related Questions
Object goes through wall (both have RigidBody and Collider attached) 2 Answers
Rigidbody2D figits when pushed against an object. 0 Answers
Rigidbody randomly going through collider 1 Answer
How to add rigid body forces and collision detection to SimpleCameraController 2 Answers
player going through walls even though it has colliders and rigidbodies 0 Answers