So I am developing this game (as part of a larger game), where the player has to collect all the coins before moving on to the next level. I want the player to when it receives a set number of button clicks from an Arduino. Every level requires a different amount of presses. So, for example, level 1 requires 1 click, level 2 requires 2 clicks etc. The issue here is that the player isn't responding to the input and I do not know why. When I tested this code using the space key, the player moved so the issue is definitely somewhere between the player and the Arduino. Any tips on how I can fix this issue will be very appreciated.
This is the Arduino script:
#include <Bounce2.h>
#include<SoftwareSerial.h>
#include <Wire.h>
// Connect each button with one connection
// to GND and the other to a digital pin.
const byte buttonPin = 2;
const byte buttonPin2 = 3;
class Button{
private:
byte m_buttonPin;
byte m_counter = 0;
unsigned long m_buttonPressTimeout;
unsigned long m_previousMillis;
public:
Button(byte buttonPin):
m_buttonPin(buttonPin),
m_counter(0),
m_buttonPressTimeout(2000), // Button press timeout in ms.
m_previousMillis(0){}
void Update(){
if(m_counter > 0 && millis() - m_previousMillis >= m_buttonPressTimeout)
{
//Serial.print("Count from Update() just before it's reset to 0 = ");
Serial.println(GetCounter());
m_counter = 0;
}
}
void IncrementCounter(){
m_counter++;
if(m_counter > 4){m_counter = 4;}
if(m_counter == 1)
{
m_previousMillis = millis();
}
}
friend void IncrementCounter(Button&);
void IncrementCounter(Button&){
IncrementCounter();
}
byte GetCounter(){
return m_counter;
}
};
Bounce buttonOneDebouncer = Bounce();
Bounce buttonTwoDebouncer = Bounce();
Button ButtonOne(buttonPin);
Button ButtonTwo(buttonPin2);
void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
buttonOneDebouncer.attach(buttonPin);
buttonTwoDebouncer.attach(buttonPin2);
buttonOneDebouncer.interval(25);
buttonTwoDebouncer.interval(25);
}
void loop(){
// Call the Update function as fast as possible.
ButtonOne.Update();
ButtonTwo.Update();
// Button one pressed.
if(buttonOneDebouncer.update()){
if(buttonOneDebouncer.fell()){
if(digitalRead(buttonPin2) == 0){
ButtonOne.IncrementCounter();
}
}
}
// Button two pressed.
if(buttonTwoDebouncer.update()){
if(buttonTwoDebouncer.fell()){
if(digitalRead(buttonPin) == 0){
ButtonOne.IncrementCounter(ButtonTwo);
}
}
}
/*if(digitalRead(buttonPin) == 0 && (digitalRead(buttonPin2) == 0))
{
Serial.println("9");
}*/
}
This is the Player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.IO.Ports;
public class PlayerControllerb : MonoBehaviour
{
public static int level;
[Header("Debug")]
[SerializeField] private PlatformProvider platformProvider;
SerialPort sp = new SerialPort("\\\\.\\COM4", 9600);
//player = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;
private void Awake()
{
OnSceneLoaded();
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneLoaded += OnSceneLoaded;
print(level);
}
public void OnSceneLoaded()
{
}
public void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
platformProvider = FindObjectOfType<PlatformProvider>();
}
public float Speed = 1;
private int currentPlatformIndex;
public Vector2 height;
public float xMin, xMax, yMin, yMax;
string value;
int amount;
void Start()
{
if (!sp.IsOpen)
{ // If the erial port is not open
sp.Open(); // Open
}
sp.ReadTimeout = 1; // Timeout for reading
print(level);
}
void Update()
{
// if (Input.GetKeyDown(KeyCode.Space) && level == 1)
// GoToNextPlatform();
if (sp.IsOpen)
{ // Check to see if the serial port is open
try
{
value = sp.ReadLine(); //Read the information
amount = int.Parse(value);
print(amount);
//transform.Translate(Speed * Time.deltaTime, 0f, 0f); //walk
//if (/*amount > 25f)*/Input.GetKeyDown(KeyCode.Space)) //jump
if (amount == 1 && level == 1)
{
//if (Input.GetKeyDown(KeyCode.Space))
//{
GoToNextPlatform();
// GetComponent<Rigidbody2D>().AddForce(2* height, ForceMode2D.Impulse);
}
if (amount == 2 && level == 2)
{
//if (Input.GetKeyDown(KeyCode.Space))
//{
GoToNextPlatform();
// GetComponent<Rigidbody2D>().AddForce(2* height, ForceMode2D.Impulse);
}
if (amount == 3 && level == 3)
{
//if (Input.GetKeyDown(KeyCode.Space))
//{
GoToNextPlatform();
// GetComponent<Rigidbody2D>().AddForce(2* height, ForceMode2D.Impulse);
}
if (amount == 4 && level == 4)
{
//if (Input.GetKeyDown(KeyCode.Space))
//{
GoToNextPlatform();
// GetComponent<Rigidbody2D>().AddForce(2* height, ForceMode2D.Impulse);
}
}
catch (System.Exception)
{
}
/*GetComponent<Rigidbody2D>().position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
);*/
}
}
[ContextMenu(nameof(GoToNextPlatform))]
public void GoToNextPlatform()
{
currentPlatformIndex++;
currentPlatformIndex = Mathf.Min(currentPlatformIndex, platformProvider.platforms.Length);
GoToPlatform(currentPlatformIndex);
}
[ContextMenu(nameof(GoToPreviousPlatform))]
public void GoToPreviousPlatform()
{
currentPlatformIndex--;
currentPlatformIndex = Mathf.Max(currentPlatformIndex, 0);
GoToPlatform(currentPlatformIndex);
}
private void GoToPlatform(int index)
{
//StopAllCoroutines();
//StartCoroutine(MoveTo(platformProvider.platforms[index].transform.position));
gameObject.transform.position = platformProvider.platforms[index].transform.position;
}
IEnumerator MoveRoutine(Vector3 target)
{
while (!Mathf.Approximately(0, Vector3.Distance(transform.position, target)))
{
yield return new WaitForFixedUpdate();
GetComponent<Rigidbody>().MovePosition(Vector3.MoveTowards(GetComponent<Rigidbody>().position, target, Time.deltaTime * Speed));
}
}
private void FixedUpdate()
{
GetComponent<Rigidbody2D>().position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
);
}
void ApplicationQuit()
{
if (sp != null)
{
{
sp.Close();
}
}
}
}
This is a picture of the console to show that the inputs are being registered
. Here is an image of the game: 
GoToNextPlatformget called in that case? Is it working correctly? \$\endgroup\$