The KY-023 Dual Axis Joystick module uses a biaxial potentiometer to control the X and Y axis. you can Control Unity3d by Arduino KY-023 Dual Axis Joystick we will use Arduino sensors with unity 3d
How to integrate Arduino Uno with Unity3d to hardware work with software make = 4D
The KY-023 Dual Axis Joystick module with ARDUINO UNO
Unity 3d Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controll : MonoBehaviour
{
float x, y, b;
void OnMessageArrived(string msg)
{
// this center value 507|520|1
Debug.Log("Message arrived: " + msg);
var array = msg.Split("|"[0]);
x = float.Parse(array[0]);
y = float.Parse(array[1]);
b = float.Parse(array[2]);
if (x > 607)
{
transform.position += new Vector3(0.1f, 0f, 0);
}
if (x < 407)
{
transform.position -= new Vector3(0.1f, 0f, 0);
}
if (y > 607)
{
transform.position += new Vector3(0f, 0.1f, 0);
}
if (y < 407)
{
transform.position -= new Vector3(0, 0.1f, 0);
}
}
void OnConnectionEvent(bool success)
{
if (success)
Debug.Log("Connection established");
else
Debug.Log("Connection attempt failed or disconnection detected");
}
}
Arduino code
int value = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(7, INPUT_PULLUP); //set pin 7 as an input and enable the internal pull-up resistor
Serial.begin(9600);
}
void loop() {
value = analogRead(A0); // read X axis value [0..1023]
//Serial.print("X:");
Serial.print(value, DEC);
value = analogRead(A1); // read Y axis value [0..1023]
Serial.print("|");
// Serial.print(" | Y:");
Serial.print(value, DEC);
value = digitalRead(7); // read Button state [0,1]
Serial.print("|");
// Serial.print(" | Button:");
Serial.println(value, DEC);
delay(100);
}
video tuturial