Find GameObjects in a circular range of a point

Hi,

To find a game object we use Find function. But I need to find the game objects that are in a circular range of a bomb exploded.

or any other way to put them away when bomb explodes.

thanks in advance

unityboy

Check collisions with a sphere that has the same radius as the explosion.
You should tag things that should not be destructible and ignore them in the list of collisions.

I am confused from this sphere. will it be attached with the particle system that i am using for explosion.

or could you give me example code for this…

thanks

Really, you would just make a sphere collider at the origin of your explosion and for each collision against appropriate objects by the sphere, process that collision how you would like. That could be outright destruction, application of force, or some other game-specific method.

This would, in a way which is rather transparent to you, pass the test for objects within a certain range to Unity internal functionality for collision testing. Not that I have seen your scripting, but I would be amazed if you could write something in script which would be as efficient as what is already in the engine.

Look into this: Unity - Scripting API: Physics.OverlapSphere

It will return an array of colliders that is touching the sphere. (only the bounding boxes, but it’s close enough I think)

Iterate through that array, check for tags to ignore and do whatever you want with the rest of the objects.

… Or go with JRaveys suggestion. Whatever you choose, please report back if you succeed so others can learn from it as well. :slight_smile:

1 Like

I did not know about the OverlapSphere. It is basically the same thing as manually making a sphere and getting collisions from it; but like I said earlier, when you can use built-in functionality, go with that. I feel quite confident that the UT programmers are better at programming than I am, so if they provide the functionality, I have more faith in their implementation than my own.

My recommendation would be to use the OverlapShere now that I have been introduced to it! The documentation says that it checks against the bounding colliders, which would be less accurate, but if you really wanted to be hardcore, you could let OverlapSphere make the short list of objects with a high probability of collision, then perform a higher fidelity collision check on objects which are sufficiently close and important enough to justify it.

One important thing about design and collisions to always remember, collisions are expensive, so good enough is usually good enough.

sorry for late I was away

I will implement and will tell what was best.

thanks

thanks deps it was better to use OverlapSphere approach.
I have got all of the game objects that are within 100 units away. I have attached this script with in the particle system that I am using to display explosion.

var Colliders : Collider[];
Colliders = Physics.OverlapSphere(transform.position, 100, 1);
for(var i : int = 0; i<=Colliders.length-1; i++)
{
        print(Colliders[i].gameObject.transform.name);
}

thanks for this…

Now I want to add a little force to put the enemy a little away. I am using the following code to get directional vector like this and then adding force to the selected game object.

if(Colliders[i].gameObject.transform.name == "Enemy")
{
var forceDirection : Vector3;
forceDirection = transform.position - Colliders[i].gameObject.transform.position;
Colliders[i].gameObject.rigidbody.AddForce(forceDirection*100);
}

but its not working to put the game object away. Any suggestion to get directional vector.??

thanks again

If you use rigidbody.AddExplosionForce, this will be calculated for you automatically.

thanks andeee

that was really good to use. But my enemy are not moved due to this function and due to the AddForce as well.

i have made the following settings with my enemy prefabe… see on image attached.

any idea ?? or any thing that I am doing wrong

i used the following code

var Colliders : Collider[];
	Colliders = Physics.OverlapSphere(transform.position, 100);
	
	for (var hit in Colliders)
	{
		if (!hit)
		continue;

		if(hit.transform.name == "Enemy(Clone)"  hit.rigidbody)
		{
			print("before : "+hit.rigidbody.velocity);
			hit.rigidbody.AddExplosionForce(1000, transform.position, 100, 500);
			print("after : "+hit.rigidbody.velocity);
		}
		
	}

i do not see any difference in print statement results.

You have the upwardsModifier value for the explosion force set to 500. This means that it behaves as though the explosion were 500 units below the object. However, the explosion radius is only 100 units, so nothing happens. Try missing out this parameter or setting it to a much lower value (maybe about 10 or so).

sorry I had started the other work of game.

Now again I came to this problem…

I applied these changes what andeeee told me.

but its not working yet :frowning:

unityboy

What is going wrong?

Thanks for reply, Actually I am unable to understand the thing that is going wrong.

I also uploaded an image of my inspector settings of enemy prefab that you may not be able to see in your browser that you can see here

I am using this code to the explosion particular system.

function Start() {
	StartBomb();
}

function StartBomb()
{
	var Colliders : Collider[];
	Colliders = Physics.OverlapSphere(transform.position, 100);
	
	for (var hit in Colliders)
	{
		if (!hit)
		continue;

		if(hit.transform.name == "Enemy1(Clone)" || hit.transform.name == "DyingCharacter1(Clone)")
		{
			hit.rigidbody.AddExplosionForce(100, transform.position, 100, 0, ForceMode.VelocityChange);
		}
		
	}
	yield new WaitForSeconds(3);
	Destroy(gameObject);
}

I think it must work for the objects that come in this range. but they dont get any explosion force. “Enemy1(Clone)” and “DyingCharacter1(Clone)” are my enemy prefabs clones that need to be added force and its coming in if statement within the loop. as enemys exist near explosion.

thanks

unityboy

use tags intead of name, that way you dont need to know the name of every object that you want to hit

tag all enemies as enemy, and check this tag