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!