A Game Can Be Modified to a Point Where It Loses It Components

In any given gage, you are belik going to uncovering dozens, if not hundreds of different objects colliding OR intersecting with one another. Hence, one of the first things you watch in Unity is how to identify the type of physical object you have touched. These are the most common shipway to manage so among beginners:

void OnTriggerEnter(Collider other) {           if(other.tag == "Enemy") {         Destruct(other.gameObject);     } }
nullif OnCollisionEnter(Collision collisionData) {          if(collisionData.collider.tag == "Enemy") {         Destroy(some other.gameObject);     } }        

Essentially, the musical theme is checking whether the object we are colliding with or touching has been labelled with an Enemy mark, before we perform any execute on the object.

Piece it is simple and impressionable-to-understand, there are better ways of characteristic objects we are colliding with.

Comparison tags more efficiently

Firstly, the tag comparison method shown above isn't the outflank way of life. It is actually to a greater extent efficient to use CompareTag(), which is gettable to both GameObjects and Components (explained below). The method can be utilised by both GameObjects and Components, which means that Collider objects bear access to the method acting too.

invalidate OnTriggerEnter(Collider otherwise) {           if(          other.CompareTag("Enemy")          ) {         Destroy(past.gameObject);     } }

Accordant to this post along Unity answers, this is wherefore CompareTag() is quicker than a direct comparison:

The key difference of opinion betwixt tag and compareTag is that compareTag does not result in a heap allocation. In Unity, retrieving string section from secret plan Objects will produce a duplicate of the train, which will need to be garbage self-contained.

The source I found reports a ~27% increase in performance of compareTag(aString) vs. using gameObject.tag == "aString";

Source: Unity 5 Game Optimization past Chris Dickinson

Past leoszeto — Wholeness Answers

In simple spoken communication, the reasonableness wherefore CompareTag() is faster is because the consumption of GameObject.tag creates an extra variable quantity that will need to be garbage composed.

Name-checking

If you have specific types of GameObjects that are unparalleled (i.e. alone 1 of it exists at any given time), you can give it a unique name and simply check its call as an alternative of its tag.

void OnTriggerEnter(Collider other) {          if(          other.name == "Player"          ) {         Destroy(other.gameObject);     } }        

In shell you don't know, on that point are a few ways you can localise the name of a GameObject.

Changing a GameObject's name
Changing a GameObject's key

Names are not the best way of identifying GameObjects, but it saves you the problem of assigning tags to foreordained objects. You evenhanded receive to make sure that the name calling tally on the nose.

GameObject names must match
Train note of pillowcase and additional whitespaces!

Article continues after the advertisement:


Component checking

The best and most ascendible way — aside far — is checking whether the GameObject has a special Component. This is how it's done:

void OnTriggerEnter(Collider other) {          if(          other.GetComponent<Enemy>()          ) {         Destroy(other.gameObject);     } }        

GetComponent() searches a GameObject and retrieves a Component onymous Enemy. If such a Component is not found, it returns zilch. When put into a qualified, it is automatically converted into a Boolean value — a nada value becomes false, and any other value becomes true.

For readability, some people may choose to make the conditional more graphic:

if( other.GetComponent<Enemy>() == null )

One benefit of checking your object in such a way is that you Don't have to make sure all your GameObjects are tagged and / or named correctly, which makes your game much easier to scale leaf. Otherwise, you would have to make over a tag for every type of GameObject in your game, and ready sure that they are all tagged correctly to duplicate this functionality.

Another benefit is that checking your GameObjects this way gives you admittance to the factor without you having to manage a separate procedure call to retrieve it.

void OnTriggerEnter(Collider strange) {               Rigidbody atomic number 37 = former.GetComponent<Rigidbody>();               if(          rb          ) {         rigidbody.velocity *= -1;     } }

If you used tag end checking, you would have to do a GetComponent() call anyway after checking the tag, which makes the chase away checking redundant. See the example below:

void OnTriggerEnter(Collider other) {          if(          some other.CompareTag("PhysicsObject")          ) {                   Rigidbody rb = other.GetComponent<Rigidbody>();          rigidbody.velocity *= -1;     } }

Additionally, because you are unable to assign multiple tags to a single GameObject, you are unable to make your conditions Thomas More taxonomic category.

             if (          separate.CompareTag("PhysicsObject") && other.CompareTag("Rocket")          )

But you can do that with Components:

            if (          other.GetComponent<Rigidbody>() && other.GetComponent<Projectile>()          )

GameObjects and Components

In Unity, everything that you create on your Scene are GameObjects. At the same time, on that point are different Components in the Scene interacting with each other day in and day out. This is because each GameObject contains four-fold Components, and each Component adds a set off of behaviours to a GameObject.

GameObjects vs Components
GameObjects vs. Components

Soh why is it that, in our examples above, other.figure, other.tag and other.GetComponent() successfully find info from the GameObjects they belong to? Note that, in these examples, other is not a GameObject, just a Collider Component.

Because Unity has configured these as shortcuts to make things more favourable, and so you don't need to write more code to achieve the same things. If these shortcuts weren't available, you would feature to access the GmaeObject from the Portion first , past access the property: e.g. other.gameObject.name, else.gameObject.label.


Clause continues after the advertisement:


A Game Can Be Modified to a Point Where It Loses It Components

Source: https://blog.terresquall.com/2020/02/checking-the-type-of-gameobject-you-are-colliding-with-in-unity/

0 Response to "A Game Can Be Modified to a Point Where It Loses It Components"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel