Check if 2 or more objects of same type is near object.
Good Evening all!
I'm making a game similar to bubble blast where you shoot a bubble to a same color bubble and it will pop. However, if the bubble that gets shot is near other bubbles of the same type, it should pop as well. I have a script that is attached to the bubbles that get shot.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TheCollision : MonoBehaviour {
private BubbleDestroyer[] theBubbleDestroyer;
private GameObject[] sameType;
private Transform[] transforms;
// Use this for initialization
void Start () {
sameType = GameObject.FindGameObjectsWithTag(this.gameObject.tag);
transforms = new Transform[sameType.Length];
for (int i = 0; i < sameType.Length; i++)
{
transforms[i] = sameType[i].transform;
}
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (this.gameObject.tag == "redBubble")
{
if (other.gameObject.tag == "redBubbleShooter")
{
this.gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
}
}
This is what I have so far. Not sure if this is the correct way to go about doing this, but I'm basically trying to connect sameType(which looks for all gameobjects with the same tag) with finding the distance from the initial bubble and setting those inactive.
Answer by Baroque · Apr 24, 2017 at 07:09 PM
You can instantly get the list of all the objects inside any circle--or in other words closer than a given distance from a point--using Physics2D.OverlapCircle. No need to create a special object just for finding collisions!
From there you can check if they're of the appropriate type, or if you're using layers carefully you can just filter for the layers you care about.
Thanks for your reply. After fidgeting and testing with OverlapCircle, I finally got it to work!
Your answer
Follow this Question
Related Questions
GameObject[] doesn't have a definition for transform/name/etc... 1 Answer
How to make FunRace3D Style Player-Target Distance with UI 0 Answers
How to create a child object inside the parent (script) and assign independent variables? 0 Answers
Why can't I find a GameObject with FindGameObjectsWithTag 2 Answers
Make object follow another object while keeping the same distance from it 1 Answer