Difference between CompareTag and LayerMask?

So I just found out that I can jump using the CompareTag with the OnCollisionStay2D if I tag all the platforms as “Ground” just like what we did with the Layer. So I’m wondering what’s the difference between using the CompareTag(“Ground”) and the LayerMask.GetLayer(“Ground”)?

The Taggy way
Then I use getKey.Space and AddForce to make it jump.

The Layer way

Don’t mind the isGrounded and the comments tho, they r just there to help me understand the code in the future.


Please refrain from posting screenshots of your code because it’s impossible to copy and paste lines from them. Instead, paste it here and format it properly.

Regarding your question, the CompareTag method refers to the tag of a game object while GetLayer refers to the layer. That’s the difference. If you name both the layer and the tag the same, that’s usually a ‘coincidence’. For further information, please refer to the API.

Is this what you wanted to know?

Regarding your comments in your code, it’s a generally a good idea to write a brief comment to explain something that might be difficult to understand. Your future self doesn’t want to waste his time reading old code and will be happy if his past self helps him understand the relevant things fast. :slight_smile:

See also:

Oh okay, well sorry for those screenshots. Anw i just wanted to know what will happen if I use Tag instead of layer in this Tilevania section? maybe no more ladder layer and platform layer, but instead I use platform tag and ladder tag. What’s the difference then?

I don’t know what will happen if you use tags instead of layers. That’s something you’ll have to test. Theoretically, you should be able to achieve the same result. The difference is the code.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms