simultaneous buttons presses skipping collisions?

So I have a pretty newbie question. I’m making a game where the character sprite will move upwards towards another gameobject it’s supposed to land on whenever a key or multiple keys are pressed (up to 7 inputs left/down/right + any combination of those keys). Now I managed to get the sprite to not miss the platforms when I put in a single input. However when I try a simutaneous press the character will sometimes just skip all collisions and just fly off the screen.

Here is the code I’m working with (C#):

using UnityEngine;
using System.Collections;

public class Collidetest : MonoBehaviour {

private BoxCollider2D LilyCol;

public int CurrentLily = 0;

public int UserInput = 0;

public float speed = 0f;

public int Score = 0;

// Use this for initialization
void Start () {
	LilyCol = GetComponent<BoxCollider2D> ();	
}

// Update is called once per frame
void Update () {
	
	transform.Translate (0, -(speed * Time.deltaTime), 0);

	if (Input.GetKeyDown (KeyCode.RightArrow)) {
		speed = -3f;
		StartCoroutine ("reactivate2D");
		UserInput = 2;

		if (CurrentLily == UserInput) {
			Score++;
		}
			
	}

	if (Input.GetKeyDown (KeyCode.LeftArrow)) {
		speed = -3f;
		StartCoroutine ("reactivate2D");
		UserInput = 1;	

		if (CurrentLily == UserInput) {
			Score++;
		}

	}

	if (Input.GetKeyDown (KeyCode.DownArrow)) {
		speed = -3f;
		StartCoroutine ("reactivate2D");
		UserInput = 3;

		if (CurrentLily == UserInput) {
			Score++;
		}

	}

	if (Input.GetKeyDown (KeyCode.LeftArrow) && Input.GetKeyDown (KeyCode.RightArrow)) 
	
	{
		
		speed = -3f;
		StartCoroutine ("reactivate2D");
		UserInput = 4;

		if (CurrentLily == UserInput) 
		
		{
			Score++;
		}

	}

	//........

	if (Input.GetKeyDown (KeyCode.LeftArrow) && Input.GetKeyDown (KeyCode.DownArrow)) 
	
	{

		speed = -3f;
		StartCoroutine ("reactivate2D");
		UserInput = 5;

		if (CurrentLily == UserInput) 
		
		{
			Score++;
		}

	}

	if (Input.GetKeyDown (KeyCode.DownArrow) && Input.GetKeyDown (KeyCode.RightArrow)) 
	
	{

		speed = -3f;
		StartCoroutine ("reactivate2D");
		UserInput = 6;

		if (CurrentLily == UserInput) 
		
		{
			Score++;
		}

	}
	

	}

void OnCollisionEnter2D(Collision2D col) 
{
	if (col.gameObject.tag == "LilyBlue") {
		speed = 2f;
		CurrentLily = 1;
	}
	if (col.gameObject.tag == "LilyRed") {
		speed = 2f;
		CurrentLily = 2;
	}
	if (col.gameObject.tag == "LilyYellow") {
		speed = 2f;
		CurrentLily = 3;
	}
	if (col.gameObject.tag == "LilyPurple") {
		speed = 2f;
		CurrentLily = 4;
	}
	if (col.gameObject.tag == "LilyGreen") {
		speed = 2f;
		CurrentLily = 5;
	}
	if (col.gameObject.tag == "LilyOrange") {
		speed = 2f;
		CurrentLily = 6;
	}


	LilyCol.enabled = !LilyCol.enabled;
}

IEnumerator reactivate2D()
{
	yield return new WaitForSeconds(0.3f);
	LilyCol.enabled = !LilyCol.enabled;
}

}

Sorry if it’s a mess slowly learning how to tidy up as well!

From what I can see in your code I have some comments + suggestions:

  1. The coroutine reactivate2D is probably doing the opposite of what you are expecting, and it is sometimes deactivating LilyCol while some other times activating it. For example: if in Frame 0 you press up, the coroutine will be launched but will yield right away for 0.3 secs. Then, during the same Frame 0, if you are also pressing down (so you are holding up and down at the same time). The coroutine will be launched again and yield for 0.3 secs. What happens after 0.3 secs? both coroutines will resume, but one of them will deactivate the collider while the other one will activate it. This is because you have: LilyCol.enabled = !LilyCol.enabled; inside the coroutine. I will suggest if you are always expecting the result of the coroutine to be that it always activates the collider, then use instead: LilyCol.enabled = true;. Note that the same problem happens if you press in sequence different inputs quickly. For example, frame 0 you press up, frame 1 you press down, since the coroutine is waiting for 0.3 secs (which is most likely higher than the time of a frame Time.deltaTime), then you have again the problem mentioned above.

  2. Same thing for the OnCollisionEnter2D. What is your intention with LilyCol.enabled = !LilyCol.enabled; if you need to deactivate the collider, you should explicitly set LilyCol.enabled = false; otherwise you can get undefined behaviours, since the coroutine is actually activating and deactivating the collider in sequence, sometimes so quick that if LilyCol is colliding with the same object, while the collider is activated and deactivated by the coroutine, the OnColliderEnter2D will get called several times.

Again, I dont know the details of what you want to achieve, so if you explain a bit more, I may be able to help you out. You may not even need that coroutine that is most likely be causing undefined collider states.