Detecting Collision Distance

Working on my first game! Woohoo!

I am a full stack web developer with some C# experience but looking for some advice on the proper way to do something.

What I am looking to do to help learn is make a metal detecting type of “game”. My thoughts are to just make a ton of gameobjects and put them below the terrain. Then when you walk over it with your metal detector I will play a sound or just output to the console for now. I want to get this functionality working correctly first.

Would it be easier to do this or possibly just to go based off X/Y/Z position of the player so there isn’t 100’s of gameobjects all over the place? I am not sure how to do this when it comes to performance.

Also would you use a OverlapSphere for distance? Say if I am standing over an object and my metal detector can see down 5 feet would that function be able to trigger a collision?

In addition to that the collision direction would just be up/down. Wouldn’t want to detect something at an angle.

As you can probably tell I am brand new because I am making no sense but any help is appreciated to help me get going in the right direction. I’m sure I can figure it out and make it work, but would rather learn the right way.

Welcome!

As somebody with existing programming experience, this sounds like a good place to start, assuming you’re going for more of a prototype/MVP kind of thing instead of a full game.

You may run into issues if you’re throwing tons of GameObjects in a scene, depending on what kind of detail they have and such. If it were me, I’d probably try to approach it using regular classes and data structures at the highest level, and only use Unity stuff where you have to.

So at the start of a game, generate the position for all your objects. If you’re going to have different types of objects, made of different materials, of various sizes, figure all that stuff out now. Throw them into a list.

public List<Item> items = new List<Item>();

void GenerateItems() {
    for (int i = 0, j = totalItems; i < j; i++) {
        items.Add(GenerateItem());
    }
}

Item GenerateItem() {
    Item item = new Item();

    // Item class has properties for things like:
    // - Name
    // - Type
    // - Size
    // - Material
    // - Position (Vector3)
    // - Etc
   
    return item;
}

At the start of the game, find the object that is closest to the player’s current position.

Item GetClosestItem(Vector3 playerPos) {
    Item closestItem = items[0];
    float closestDist = Vector3.Distance(closestItem.position, playerPos);

    for (int i = 1, j = items.Count(); i < j; i++) {
        float curItemDist = Vector3.Distance(items[i].position, playerPos);
        if (curItemDist < closestDist) {
            closestItem = items[i];
            closestDist = curItemDist;
        }
    }

    return closestItem;
}

Use the position for that item to create a new GameObject in the scene with colliders and what have you. When the player gets close enough for detection to happen, do all your stuff there. As the player moves, you’ll have to re-run the GetClosestItem method to update the item they’re searching for, so you’ll have to determine what the right frequency is, there.

When you change what the closest item is, delete the GameObject for the old one before spawning the new one. And when an item is uncovered, remove it from the items list to avoid throwing it back into the world again.

Sounds like a fun little project! Good luck!

1 Like

Outstanding!

Thanks for heading me in the right direction. Some of that looks familiar so I am sure I can use and modify as needed.

Awesome. Keep us updated on your progress. I, for one, want to see this get made.

1 Like

A metal detecting type game?
Wooah! That sounds interesting.