# How to know if two colliders are touching each other

I am making a movement script and I want to make it so if you touch the colliders in the `dangerColliders` array then you will be knocked back and the health decreases. How would you make an if statement to see if the 2 colliders are touching. Here is my code:

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

public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;

public Rigidbody2D rb;

Vector2 movement;

public float health;

public Collider2D[] dangerColliders;

// Update is called once per frame
void Update()
{
// Input
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}

void FixedUpdate()
{
// Movement
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);

foreach (Collider2D danger in dangerColliders)
{
// I need an if statement here to check if the collider
// in the gameObject where this script is is touching
// the danger variable
}
}
}
``````

There are quite a lot of methods, I made 2 of them:

1. You can avoid using the colliders at all
``````public Transform[] dangerColliders;
public float dangerForce = 10;

void FixedUpdate()
{
//...
CheckForCollision();
}
private void CheckForCollision()
{
for (int i = 0; i < dangerColliders.Length; i++)
{
Vector2 dangerPosition = dangerColliders[i].position;
dangerPosition.y = transform.position.y; //I set the Y pos to the player pos so you dont break the code by changing Y
float distance = Vector3.Distance(transform.position, dangerPosition);
float activationDistance = transform.localScale.x / 2 + dangerColliders[i].localScale.x/2;
if (distance <= activationDistance)
{
}
}
}
``````
1. You can set IsTrigger to true on the BoxCollider2D of the danger objects and then you add a script to them
``````public class Wall : MonoBehaviour
{
public float dangerForce = 10;
private bool playerColliding;
private Rigidbody2D player;

public bool pushRight; //You have to say which way the wall should push

public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player")) //You have to check if it is the player
{
player = collision.GetComponent<Rigidbody2D>(); //Cache the player for better performance
playerColliding = true;
}
}
public void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player")) //You have to check if it is the player
playerColliding = false;
}
private void FixedUpdate()
{
if (playerColliding)
player.AddForce(((pushRight) ? 1 : -1) * player.transform.right * dangerForce);
}
}
``````

I prefer the first one because of the better performance and it’s also easier to add walls.
If you really want to go with the second one, remember to set the player’s tag to “Player”