Please help with the script: If crouch not kill if !crouched kill

I wanna do , that if the Bunny collide with the player (Tag “Player”) while the player is crouching that the Bunny cant collide with the player (walk trough the Player) but if the bunny collides while the palyer isn’t crouching the player gets destroyed and you get teleportet in a other scene. But it doesnt work. Can someone help me please? This is the Script for the Bunny:

using UnityEngine;
using UnityEngine.SceneManagement;

public class BunnyAI : MonoBehaviour
{
public float speed = 8f;
public float right = 129.05f;
public float left = 61.1f;
private Vector3 rotation;

private void Start()
{
    right = transform.position.x + right;
    left = transform.position.x - left;
    rotation = transform.eulerAngles;
}

private void Update()
{
    transform.Translate(Vector2.left * speed * Time.deltaTime);
    if (transform.position.x < left)
    {
        transform.eulerAngles = rotation - new Vector3(0, 180, 0);
    }
    if (transform.position.x > right)
    {
        transform.eulerAngles = rotation;
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        PlayerMovement player = collision.gameObject.GetComponent<PlayerMovement>();
        if (player != null && !player.isCrouching)
        {
            // Spieler wird deaktiviert
            collision.gameObject.SetActive(false);

            // Spieler wird in eine neue Szene teleportiert
            SceneManager.LoadScene("NewSceneName");
        }
    }
}

}

And this from the Player:

using UnityEngine;
using UnityEngine.UI;

public class MobilePlayerController : MonoBehaviour
{
[SerializeField] private float moveSpeed;
[SerializeField] private float jumpForce;
[SerializeField] private float sprintSpeed;
[SerializeField] private float sprintDuration;
[SerializeField] public float sprintCooldown;
[SerializeField] private float crouchSpeed;
[SerializeField] public float crouchHeight = 0.2487039f;

private Animator animator;
private Rigidbody2D rb;
private bool isGrounded;
private bool isJumping;
private bool isCrouching;
private bool isSprinting;
private float sprintTimer;
private float sprintCooldownTimer;
private const string GROUND_TAG = "Ground";

public Button jumpButton;
public Button moveLeftButton;
public Button moveRightButton;
public Button crouchButton;
public Button sprintButton;

private bool isMovingLeft;
private bool isMovingRight;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();
    animator = GetComponent<Animator>();

    // Zuweisung der Aktionen zu den Buttons
    jumpButton.onClick.AddListener(Jump);
    moveLeftButton.onClick.AddListener(ToggleMoveLeft);
    moveRightButton.onClick.AddListener(ToggleMoveRight);
    crouchButton.onClick.AddListener(Crouch);
    sprintButton.onClick.AddListener(StartSprinting);
}

private void Update()
{
    // Steuere die Walking-Animation basierend auf der Horizontalbewegung
    if (isMovingLeft)
    {
        MoveLeft();
    }
    else if (isMovingRight)
    {
        MoveRight();
    }
    else
    {
        StopMoving();
    }

    // Steuere die Jump-Animation basierend auf dem Sprungstatus
    animator.SetBool("IsJumping", isJumping);

    // Steuere die Sprint-Animation basierend auf dem Sprintstatus
    animator.SetBool("IsSprinting", isSprinting);

    // Steuere den Sprint-Cooldown
    if (sprintCooldownTimer > 0f)
    {
        sprintCooldownTimer -= Time.deltaTime;
    }
}

private void Jump()
{
    if (isGrounded)
    {
        isJumping = true;
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }
}

private void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.CompareTag(GROUND_TAG))
    {
        isGrounded = true;
        isJumping = false;
    }
}

private void OnCollisionExit2D(Collision2D other)
{
    if (other.gameObject.CompareTag(GROUND_TAG))
    {
        isGrounded = false;
    }
}

private void ToggleMoveLeft()
{
    if (isMovingLeft)
    {
        isMovingLeft = false;
        StopMoving();
    }
    else
    {
        isMovingLeft = true;
        isMovingRight = false;
        animator.SetBool("IsWalking", true);
        transform.rotation = Quaternion.Euler(0f, 180f, 0f);
    }
}

private void ToggleMoveRight()
{
    if (isMovingRight)
    {
        isMovingRight = false;
        StopMoving();
    }
    else
    {
        isMovingRight = true;
        isMovingLeft = false;
        animator.SetBool("IsWalking", true);
        transform.rotation = Quaternion.Euler(0f, 0f, 0f);
    }
}

private void StopMoving()
{
    isMovingLeft = false;
    isMovingRight = false;
    animator.SetBool("IsWalking", false);
    rb.velocity = new Vector2(0f, rb.velocity.y);
}

private void MoveLeft()
{
    if (!isCrouching)
    {
        rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
    }
}

private void MoveRight()
{
    if (!isCrouching)
    {
        rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
    }
}

private void Crouch()
{
    isCrouching = !isCrouching;

    if (isCrouching)
    {
        transform.localScale = new Vector2(transform.localScale.x, crouchHeight);
        rb.velocity = new Vector2(rb.velocity.x * crouchSpeed, rb.velocity.y);
    }
    else
    {
        transform.localScale = new Vector2(transform.localScale.x, 0.4487039f);
    }
}

private void StartSprinting()
{
    if (!isSprinting && sprintCooldownTimer <= 0f)
    {
        isSprinting = true;
        sprintTimer = sprintDuration;
        sprintCooldownTimer = sprintCooldown;
    }
}

private void FixedUpdate()
{
    if (isSprinting)
    {
        sprintTimer -= Time.fixedDeltaTime;

        if (sprintTimer <= 0f)
        {
            isSprinting = false;
            rb.velocity = new Vector2(rb.velocity.x / 1.5f, rb.velocity.y);
        }
        else
        {
            if (isMovingLeft)
            {
                rb.velocity = new Vector2(-sprintSpeed, rb.velocity.y);
            }
            else if (isMovingRight)
            {
                rb.velocity = new Vector2(sprintSpeed, rb.velocity.y);
            }
        }
    }
    else
    {
        if (isMovingLeft)
        {
            rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
        }
        else if (isMovingRight)
        {
            rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
        }
    }
}

}

@GalaxyCat810 - I’m assuming here that you want to allow the Bunny to collide with the Player only if the Player is not crouching. If the Player is crouching, the Bunny should be able to pass through the Player without triggering a collision?

You need to manipulate the physics layers of your game objects (in this case, the Player and the Bunny) in Unity. Something like this:

Create two layers in the Unity editor. Let’s name them “PlayerStanding” and “PlayerCrouching”.

Set your Player to be in the “PlayerStanding” layer by default.

When the Player crouches, change the Player’s layer to “PlayerCrouching” using code.

Set up your Physics2D collision matrix such that “Bunny” and “PlayerStanding” can collide, but “Bunny” and “PlayerCrouching” cannot. This can be set in the Unity editor under Edit > Project Settings > Physics2D.

In your MobilePlayerController class, add this code inside your Crouch() method to change the layer of the Player when crouching:

private void Crouch()
{
    isCrouching = !isCrouching;
    if (isCrouching)
    {
        gameObject.layer = LayerMask.NameToLayer("PlayerCrouching");
        transform.localScale = new Vector2(transform.localScale.x, crouchHeight);
        rb.velocity = new Vector2(rb.velocity.x * crouchSpeed, rb.velocity.y);
    }
    else
    {
        gameObject.layer = LayerMask.NameToLayer("PlayerStanding");
        transform.localScale = new Vector2(transform.localScale.x, 0.4487039f);
    }
}

And in your BunnyAI class, keep the OnTriggerEnter2D as it is since now the layer change will handle the interaction when Player is crouching.

Remember to add these layers in your Unity editor and setup your Physics2D collision matrix accordingly. This should help the Bunny to walk through the Player when the Player is crouching.

Let me know if this gets you closer to an answer.