Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Which design pattern should I use?

Discussion in 'Scripting' started by ducloc96, Mar 13, 2022.

  1. ducloc96

    ducloc96

    Joined:
    Mar 18, 2018
    Posts:
    26
    I have 3 types of weapon(A,B,C) and 3 types of monsters (D,E,F). Each type of weapon can only interact with certain types of monsters. For example, bullet A can only interact with monsters D, E, bullet B can only interact with monsters E, F,... So can I ask how should I handle this case? Thank you very much
     
  2. HazelMesh

    HazelMesh

    Joined:
    Mar 3, 2022
    Posts:
    7
    If under "interacting" you mean collision detection, unity have Layer system which is useful because the physics system won't even take into account the collision which aren't as your setup.

    So at the inspector you can find layer field, and then you can create layer for each weapon type AND for each enemy type.
    So for example you create layerA, layerB, and layerC for the weapons , and layer D, E ,F for each enemy.
    And the simply go to Edit-> Project settings-> Physics and at very bottom you can find the "Layer Collision Matrix", and the rest I think is self explanatory.

    The problem could only occur if you a large quantity of weapon types and enemy types (because limit of layers is 32, also it would look unorganized).
    The solution would be just checking is the collision possible with simple IF function (in onCollisionEnter).
    If your collision scripts is at the weapon then check (if enemy.type==D||enemy.type==E), regardless of the bullet type.
     
    Lethn likes this.
  3. ducloc96

    ducloc96

    Joined:
    Mar 18, 2018
    Posts:
    26
    "interacting" means there is a collision and there is handling at the time of the collision. For example, when bullets "interacting" with monsters, the bullets will be destroyed.
    So in the case that in addition to colliding with the monster, but also being destroyed when colliding with the platform, how should I handle it?
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    If your "bullets" are physics objects, you can use OnCollisionEnter or OnTriggerEnter, and check the collided object.

    In the following pseudocode, I am assuming:
    - an enum of bullet types
    - a Bullet class which contains the basic properties of a bullet, most important in this case being its bullet type
    - your bullets are physical projectiles with trigger collision.
    Code (CSharp):
    1. //on enemy
    2. BulletType weakness = BulletType.ice
    3.  
    4. void OnTriggerEnter(Collider col){
    5.     Bullet bullet = col.getComponent<Bullet>();
    6.     if(bullet.bulletType == weakness){
    7.         //Oh no, I take damage
    8.     }
    9. }
    Code (CSharp):
    1. //on bullet
    2. void OnTriggerEnter(Collider col){
    3.     //some visual effect for the bullet disintegrating
    4.     Destroy(this.gameObject);
    5. }
    This has nothing to do with design patterns, this is just making use of the collision functions Unity has provided. In my snipplets, the bullet is responsible for destroying itself, while the enemy is responsible for determining how it reacts to bullets.
     
    Bunny83 likes this.