Чем заменить тег?

В месте collision.gameObject.layer == _groundLayer с тегом работает(то есть так collision.gameObject.tag == "Ground") а без него не работает.

Что сделать чтобы чек заземления работал без тегов в коде?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerMover : MonoBehaviour
{
    [SerializeField] private float _speed = 1;
    [SerializeField] private float _jumpForce = 1;
    [SerializeField] private LayerMask _groundLayer = 9;
    private Rigidbody2D _rigidbody2D;
    private bool _isGrounded;
    private Vector3 _moveVector => new Vector2(Input.GetAxis("Horizontal"), 0);
    private void Start()
    {
        _rigidbody2D = GetComponent<Rigidbody2D>();

        if (_groundLayer == gameObject.layer)
            Debug.LogError("Player SortingLayer must be different from Ground SortingLayer.");
    }
    private void FixedUpdate()
    {
        MoveLogic();
        JumpLogic();
    }
    private void MoveLogic()
    {
        _rigidbody2D.AddForce(_moveVector * _speed, ForceMode2D.Impulse);
    }
    private void JumpLogic()
    {
        if (_isGrounded && (Input.GetAxis("Jump") > 0))
        {
            _rigidbody2D.AddForce(Vector3.up * _jumpForce, ForceMode2D.Impulse);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        IsGroundedUpdate(collision, true);
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        IsGroundedUpdate(collision, false);
    }
    private void OnTriggerStay2D(Collider2D collision)
    {
        Debug.Log(collision);
    }
    private void IsGroundedUpdate(Collision2D collision, bool value)
    {
        if (collision.gameObject.layer == _groundLayer`)
        {
            _isGrounded = value;
        }
    }
}

Ответы (1 шт):

Автор решения: BlowTein
collision.gameObject.tag == "Ground"

можно заменить на

collision.gameObject.layer.Equals(_groundLayer.value)

в случае если поле _groundLayer (это тип LayerMask) статическое

→ Ссылка