- Home /
Two Objects Touching?
What is the best way to check if two objects, both with colliders, may have spawner on top of or next to each other, I tried using an algorithm where I find the short distance from one object to the closest tagged object however all the objects share the same tag so it always returns itself as the closest object?
Is there a way I can exclude the object the script is on from the array which is being created by finding tagged objects?
OR
Is there a way I can check collisions for objects that instantiate on top of or near to each other? My biggest problem with the collision functions in this case is that they don't actually collide (the objects) do they need a rigidbody to work properly? Ill post my script bellow nicely commented so people can understand.
var thisG : GameObject;
var enemies : GameObject[];
var closestEnemy : GameObject;
var dister : float = 1000;
var dist : float ;
var destroyer : float = 0.4;
var timer : float = 0;
function Start () {
}
function Update () {
//The timer is to allow everything to spawn first
timer += Time.deltaTime;
// Creates an array of all the tagged objects
enemies = GameObject.FindGameObjectsWithTag("arno");
if(timer > 1){
if (enemies.length > 0){
var closestEnemy = enemies[0];
var dister = Vector3.Distance(thisG.transform.position , enemies[0].transform.position);;
for (var m = 0; m < enemies.length;m++){
var tempDist = Vector3.Distance(thisG.transform.position , enemies[m].transform.position);
if (tempDist == 0){
}else{
if (tempDist < dister){
closestEnemy = enemies[m];
//used to find the closest enemy
}
}
}
dist = Vector3.Distance(thisG.transform.position , closestEnemy.transform.position);
}else{
dist = 1000;
}
if(dist == 0 ){
}else{
if (dist < destroyer){
// if the enemy is to close to another enemy on spawn it destroys it
Destroy(closestEnemy);
Destroy(thisG);
}
}
}
}
function OnCollisionEnter(hit : Collision) { // This whole function doesn't work i just kept it in the code to see if someone can edit it
if (hit.gameObject.tag == "arno"){
Destroy(thisG);
}
}
I don't know about the rest, but you can check for "yourself" with something like if(gameObject==enemies[i]) continue;
Continue is a trick that sort of puts the rest of the loop in an else -- jumps right to the top of the loop for i+1. It can save a lot of {}'s.
thisG
is redundant -- are you using it to save lookup time? If so, set it in Start: thisG=gameObject;
Thanks for the "ThisG = gameObject" I hadn't refractored the code for elegance yet I am trying the continue thing, hopefully it works thanks :)
Problem solved!! thanks, the continue combined with some closer exa$$anonymous$$ation of the script helped me! if you post an answer ill be happy to select it, just be sure to include the fact that i was missing a second check for "dister" after I had changed the closest enemy :)
Your answer
Follow this Question
Related Questions
How to spawn an object continually while floating vertically like a bubble 1 Answer
Checking Collision on instantiated object 0 Answers
Detecting collision from Instantiated objects 1 Answer
How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer
How can I instantiate rigid bodies on top of each other without them exploding away? 2 Answers