Go Down

Topic: Controlling a game in Unity using Arduino (Read 1 time) previous topic - next topic

shradha

Hi All

I am trying to create a simple game of moving a ball on the screen and collecting coins on the way.  I want to control the movement of my player, i.e the ball using buttons on the Arduino board, similar to keys on the keyboard. Both my arduino and unity code are not showing any errors but when I try and run my game using the buttons on the arduino nothing happens. I am using the Serial Port Class to connect Unity and Arduino.
Can you please take a look and give some insight. Thanks!

My Arduino code:

const int buttonPin01 = 6;
const int buttonPin02 = 7;
const int buttonPin03 = 8;
const int buttonPin04 = 9;

void setup()
{
  Serial.begin(9600);
 
  pinMode(buttonPin01, INPUT);
  pinMode(buttonPin02, INPUT);
  pinMode(buttonPin03, INPUT);
  pinMode(buttonPin04, INPUT);
 
  digitalWrite(buttonPin01, HIGH);
  digitalWrite(buttonPin02, HIGH);
  digitalWrite(buttonPin03, HIGH);
  digitalWrite(buttonPin04, HIGH);
 
}

void loop()
{
  if(digitalRead(buttonPin01) == LOW)
  {
    Serial.write(1);
    Serial.flush();
    delay(20);
  }
 
  if(digitalRead(buttonPin02) == LOW)
  {
    Serial.write(2);
    Serial.flush();
    delay(20);
  }
 
if(digitalRead(buttonPin03) == LOW)
  {
    Serial.write(3);
    Serial.flush();
    delay(20);
  }
 
  if(digitalRead(buttonPin04) == LOW)
  {
    Serial.write(4);
    Serial.flush();
    delay(20);
  }
}

My Unity Code:

using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class PlayerController : MonoBehaviour {
   
   public float speed;
   
   SerialPort sp = new SerialPort("COM3", 9600);
   public GUIText countText;
   public GUIText winText;
   private int count;
   
   void Start()
   {
      sp.Open();
      sp.ReadTimeout = 1;
      count = 0;
      SetCountText();
      winText.text = "";
   }
      
   void Update()
   {
      
       if(sp.IsOpen)
       {
         try
         {
             MoveObject(sp.ReadByte());
             print(sp.ReadByte());
         }
         catch (System.Exception)
         {
        
         } 
       } 
      
    if (Input.GetKeyDown(KeyCode.Escape))
      {
        Application.Quit();
        }

   }
    void MoveObject(int Direction)
   {
      if(Direction == 1)
       {
          rigidbody.AddForce(Vector3.left * speed * Time.deltaTime);
       }
   
       if(Direction == 2)
       {
         rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
       }
   }
   
       if(Direction == 3)
       {
           rigidbody.AddForce(Vector3.top * speed * Time.deltaTime);
       }
   
       if(Direction == 4)
       {
          rigidbody.AddForce(Vector3.bottom * speed * Time.deltaTime);
       }
   
   void OnTriggerEnter(Collider other)
   {
      if(other.gameObject.tag == "PickUp")
      {
         other.gameObject.SetActive(false);
         count = count + 1;
         SetCountText();
      }
         
   }
   
   void SetCountText()
   {
      countText.text = "Count: " + count.ToString();
      if(count >= 12)
      {
         winText.text = "YOU WIN!";
      }
   }
}

PaulS

I'd start with the Arduino code. Change the Serial.write() to Serial.print(), and print something recognizable when each switch is pressed. Open the serial monitor to see the output. No output == hardware problem. Output == Unity problem.

On the Unity side, can you add output statements, to echo what you read from the serial port?

Go Up