Search Unity

Collision Decisions - on the projectiles, or on the targets?

Discussion in 'Getting Started' started by sniderthanyou, Feb 2, 2020.

  1. sniderthanyou

    sniderthanyou

    Joined:
    Jan 31, 2020
    Posts:
    2
    I'm working through the Create with Code course, Project Unit 2 - Basic Gameplay, Lesson 2.4 - Collision Decisions. In this lesson, we attach Box Colliders to animals and projectiles, and then we add a DetectCollisions script to each of the animals. In this DetectCollisions script, we override OnTriggerEnter to remove both colliding objects. I have a few objections here:

    1) The animals all run at different speeds. If one animal runs into another, the animals will be removed.
    2) I have to add this script to each of the animal prefabs, whereas I would only have to add it to the one projectile prefab.

    Why does the tutorial instruct us to add this script to the animal prefabs, rather than the one projectile prefab? Is there something I just haven't learned yet to explain this? When designing games, how do YOU usually decide whether to perform collision detection on within the projectiles, or within the targets?

    Thank you!

    Erik
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    You can test for 'animal' objects in the OnCollisionEnter method and not destroy when true.

    There is nothing odd about that. Get use to adding scripts to gameobjects. Adding scripts to prefabs and instantiating many of those prefabs is the way you do it in Unity. Avoid large 'Master Scripts' that do everything, let each gameobject control itself.

    When you write it you get to do anything you want. Why don't you finish the tutorial, maybe he changes things later on. The purpose of the tutorials is to give new developers a chance to follow a structured lesson while learning to use the Editor, Console, Scripting environment, and Unity workflow. Keep at it.
     
    JoeStrout likes this.
  3. sniderthanyou

    sniderthanyou

    Joined:
    Jan 31, 2020
    Posts:
    2
    Thanks Bill!

    2) I'm happy to add scripts to objects, but it seemed odd to add the same script to three different objects, when adding it to just the one projectile would have sufficed. Perhaps this example was just so simplified that it didn't matter.

    I also asked this question on Discord, and they suggested that in a fuller featured game, each animal might want to do something slightly different when hit by a projectile, rather than the same uniform behavior everywhere. Or that each different kind of projectile might behave differently on collision (even though there's only one type of projectile right now), and that could impact where you want to attach the collision detection code.

    If you have any further advice on how to make this sort of determination going forward, I'd love to hear it.

    Thanks again!