Skip to main content
added 291 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class could includeis a wrapper around an enum with all the factions an object can belong to:

When you add this component to a GameObject, you will have a dropdown menu in its inspector where you can pick the faction it belongs to between "CATS", "DOGS" and "HAMSTERS".

You don't need to create a separate MonoBehaviour for this, by the way. You could just add this enum and public variable to an existing one if you have one where it makes sense. But I don't know what else you got in your project, so let's go with a dedicated behaviour for this.

How that method determines which factions are enemies and which are not is outside of the scope of this answer, because the logic for this could get quite complex depending on what exactly your requirements are.

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class could include an enum with all the factions an object can belong to:

When you add this component to a GameObject, you will have a dropdown menu in its inspector where you can pick the faction it belongs to between "CATS", "DOGS" and "HAMSTERS".

How that method determines which factions are enemies and which are not is outside of the scope of this answer.

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class is a wrapper around an enum with all the factions an object can belong to:

When you add this component to a GameObject, you will have a dropdown menu in its inspector where you can pick the faction it belongs to between "CATS", "DOGS" and "HAMSTERS".

You don't need to create a separate MonoBehaviour for this, by the way. You could just add this enum and public variable to an existing one if you have one where it makes sense. But I don't know what else you got in your project, so let's go with a dedicated behaviour for this.

How that method determines which factions are enemies and which are not is outside of the scope of this answer, because the logic for this could get quite complex depending on what exactly your requirements are.

added 131 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

I expect that the part you want to optimize is collision.collider.name.Equals("") because you used the Unity profilerUnity Profiler and found out that this particular line of code takes up a considerablean unacceptable amount of execution time (I would find that unlikely, though). You also only might just want to find out if the other object is friend, enemy or neutral and consider the name of the collider an inappropriate method of doing so (Which I would consider a much better reason).

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class could include an enum with all the factions thean object belongscan belong to:

This code of course assumes that all 3 factions we have in this game are mortal enemies who will always fight each other. But this might not always be the case. For example, what if the dogs and the hamsters have fored anforged a temporary alliance against the cats? In that case it might make sense to add a helper method bool IsEnemyOf(FactionMembership other) to FactionMembership.

How that function determines who is allied with whom is outside of the scope of this answer.

How that method determines which factions are enemies and which are not is outside of the scope of this answer.

I expect that the part you want to optimize is collision.collider.name.Equals("") because you used the Unity profiler and found out that this particular line of code takes up a considerable amount of execution time. You also only might just want to find out if the other object is friend, enemy or neutral.

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class could include an enum with all the factions the object belongs to:

This code of course assumes that all 3 factions we have in this game are mortal enemies who will always fight each other. But this might not always be the case. For example, what if the dogs and the hamsters have fored an alliance against the cats? In that case it might make sense to add a helper method IsEnemyOf(FactionMembership other) to FactionMembership.

How that function determines who is allied with whom is outside of the scope of this answer.

I expect that the part you want to optimize is collision.collider.name.Equals("") because you used the Unity Profiler and found out that this particular line of code takes up an unacceptable amount of execution time (I would find that unlikely, though). You also might just want to find out if the other object is friend, enemy or neutral and consider the name of the collider an inappropriate method of doing so (Which I would consider a much better reason).

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class could include an enum with all the factions an object can belong to:

This code of course assumes that all 3 factions we have in this game are mortal enemies who will always fight each other. But this might not always be the case. For example, what if the dogs and the hamsters have forged a temporary alliance against the cats? In that case it might make sense to add a helper method bool IsEnemyOf(FactionMembership other) to FactionMembership.

How that method determines which factions are enemies and which are not is outside of the scope of this answer.

Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

I expect that the part you want to optimize is collision.collider.name.Equals("") because you used the Unity profiler and found out that this particular line of code takes up a considerable amount of execution time. You also only might just want to find out if the other object is friend, enemy or neutral.

Let's also assume that you have more than two factions in your game. Because if you only have players and enemies, you don't need an enum to store faction membership. A bool would be much simpler.

In order to do that, you could add a component FactionMembership to each object in your game (or at least those where the friend/foe status is relevant). That class could include an enum with all the factions the object belongs to:

public FactionMembership: MonoBehaviour {
   
     public enum Faction {
          CATS,
          DOGS,
          HAMSTERS
     }

     public Faction faction;
}

When you add this component to a GameObject, you will have a dropdown menu in its inspector where you can pick the faction it belongs to between "CATS", "DOGS" and "HAMSTERS".

Now back to your collision function. I assume you want to find out if the object belongs to a different faction. In that case you would do this:

private void OnCollisionEnter2D(Collision2D collision)
{
    // get the faction membership components of both objects.
    // note: if you call this function a lot, you might want to 
    // store myFactionMembership in a private class-level variable
    // which you read in Start to improve performance
    FactionMembership myFactionMembership = GetComponent<FactionMembership>();
    FactionMembership otherFactionMembership = collision.gameObject.GetComponent<FactionMembership>();

    // do both objects in this collision actually have a faction membership? 
    // If not, then we are not interested in this collision.
    if(myFactionMembership == null || otherFactionMembership == null) {
         return;
    }
    // check if we have encountered an enemy
    if (myFactionMembership.faction != otherFactionMembership.faction) {
          Console.log("Enemy encountered!");
    }

}

This code of course assumes that all 3 factions we have in this game are mortal enemies who will always fight each other. But this might not always be the case. For example, what if the dogs and the hamsters have fored an alliance against the cats? In that case it might make sense to add a helper method IsEnemyOf(FactionMembership other) to FactionMembership.

How that function determines who is allied with whom is outside of the scope of this answer.

    FactionMembership myFactionMembership = GetComponent<FactionMembership>();
    FactionMembership otherFactionMembership = collision.gameObject.GetComponent<FactionMembership>();
    if(myFactionMembership == null || otherFactionMembership == null) {
         return;
    }
    
    if (myFactionMembership.IsEnemyOf(otherFactionMembership)) {
          Console.log("Enemy encountered!");
    }