- Home /
Unity only paying attention to one trigger check at once.
I am creating a collision system for my movement system, since applying 2dbox colliders wasn't working with the movement code I started with.
Basically, I have the player game object with four child objects, one for each cardinal direction, and these child objects each have a 2dboxcollider with isTrigger enabled. They each have a duplicate of the script below that communicates with the player script using simple Bools to check if the child object is colliding with anything. If it is, then the player's movement input is ignored for that direction.
My issue is that, while the system works perfectly when contacting a single wall, if the player is contacting two walls simultaneously (corner or tight corridor) then unity is ignoring whichever collision happened first and only updating the collision bool of the latest collision. This allows the player to easily clip through walls if they position themselves properly.
Here's my child object code (which is duplicated with a number increment):
public bool isColliding = false;
public void OnTriggerEnter2D(Collider2D enterCol)
{
isColliding = true;
}
void OnTriggerExit2D(Collider2D exitCol)
{
isColliding = false;
}
Here's my player code:
// Setting variables for movement
private bool isMoving;
private Vector2 origPos, targetPos;
private float timeToMove = 0.5f;
// Each of the empty objects around the Player Prefab
public GameObject check;
public GameObject check1;
public GameObject check2;
public GameObject check3;
// Directional Bools for empty object's collision
public bool eastColliding;
public bool southColliding;
public bool westColliding;
public bool northColliding;
// Holders for external Bools
bool localIsColliding;
bool localIsColliding1;
bool localIsColliding2;
bool localIsColliding3;
void FixedUpdate()
{
// Connecting local Bools to external Bools
localIsColliding = check.GetComponentInChildren<CheckCollision>().isColliding;
localIsColliding1 = check1.GetComponentInChildren<CheckCollision1>().isColliding1;
localIsColliding2 = check2.GetComponentInChildren<CheckCollision2>().isColliding2;
localIsColliding3 = check3.GetComponentInChildren<CheckCollision3>().isColliding3;
// Checking if colliding and setting directional Bools accordingly
// East
if (localIsColliding)
{
eastColliding = true;
Debug.Log("East Colliding = True");
}
else
{
eastColliding = false;
}
// West
if (localIsColliding1)
{
westColliding = true;
Debug.Log("West Colliding = True");
}
else
{
westColliding = false;
}
// North
if (localIsColliding2)
{
northColliding = true;
Debug.Log("North Colliding = True");
}
else
{
northColliding = false;
}
// South
if (localIsColliding3)
{
southColliding = true;
Debug.Log("South Colliding = True");
}
else
{
southColliding = false;
}
// Move up IF !isMoving and No collision detected.
if (Input.GetKey(KeyCode.W) && !isMoving && northColliding == false)
StartCoroutine(MovePlayer(Vector2.up));
// Move left IF !isMoving and No collision detected.
if (Input.GetKey(KeyCode.A) && !isMoving && westColliding == false)
StartCoroutine(MovePlayer(Vector2.left));
// Move down IF !isMoving and No collision detected.
if (Input.GetKey(KeyCode.S) && !isMoving && southColliding == false)
StartCoroutine(MovePlayer(Vector2.down));
// Move right IF !isMoving and No collision detected.
if (Input.GetKey(KeyCode.D) && !isMoving && eastColliding == false)
StartCoroutine(MovePlayer(Vector2.right));
}
private IEnumerator MovePlayer(Vector2 direction)
{
isMoving = true;
float elapsedTime = 0;
origPos = transform.position;
targetPos = origPos + direction;
while(elapsedTime < timeToMove)
{
transform.position = Vector2.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
elapsedTime += Time.deltaTime;
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
Answer by theoharisalexander · Jul 04, 2021 at 06:23 PM
I've now realized I'm an idiot.
All I had wrong was that I was using OnTriggerEnter2D instead of OnTriggerStay2D. Switching to the latter fixed my issue entirely.
I'm leaving the post up in case anyone else has the same issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220613035810im_/https://answers.unity.com/themes/thub/images/avi.jpg)