Hello
I’m fairly new at this and is trying to make a simple race game along the line of Star Wars pod racing.
But I can’t get my arduino to work properly and I don’t know what’s wrong. I saw a video online of a guy, who made his arduino work with unity, so I sort of did what he did and just chaged some of the code.
My code is working if I use the “F” and “J” buttons but the arduino won’t.
This is the code I’ve got so far:
{
if (Input.GetKey(KeyCode.J) Input.GetKey(KeyCode.F))
{
float translation = Input.GetAxis("Vertical") * acc * curSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, curSpeed * Time.deltaTime);
curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
curSpeed = curSpeed + acc;
}
if (Input.GetKey(KeyCode.F) !Input.GetKey(KeyCode.J))
{
transform.Rotate(0, rotaSpeed * Time.deltaTime, 0);
float translation = Input.GetAxis("Vertical") * acc * curSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, curSpeed * Time.deltaTime);
curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
}
if (Input.GetKey(KeyCode.J) !Input.GetKey(KeyCode.F))
{
transform.Rotate(0, -rotaSpeed * Time.deltaTime, 0);
float translation = Input.GetAxis("Vertical") * acc * curSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, curSpeed * Time.deltaTime);
curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
}
}
void moveObject(int direction)
{
if (direction == 1)
{
transform.Rotate(0, rotaSpeed * Time.deltaTime, 0);
float translation = Input.GetAxis("Vertical") * acc * curSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, curSpeed * Time.deltaTime);
curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
}
if (direction == 2)
{
transform.Rotate(0, -rotaSpeed * Time.deltaTime, 0);
float translation = Input.GetAxis("Vertical") * acc * curSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, curSpeed * Time.deltaTime);
curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
}
if (direction == 3)
{
float translation = Input.GetAxis("Vertical") * acc * curSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, curSpeed * Time.deltaTime);
curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
curSpeed = curSpeed + acc;
}
}
}
And this is my arduino code:
const int buttonPinLeft=8;
const int buttonPinRight=7;
void setup()
{
Serial.begin(9600);
pinMode(buttonPinLeft,INPUT);
pinMode(buttonPinRight,INPUT);
digitalWrite(buttonPinLeft,HIGH);
digitalWrite(buttonPinRight,HIGH);
}
void loop()
{
if(digitalRead(buttonPinLeft) == LOW)
{
Serial.write(2);
Serial.flush();
delay(20);
}
if(digitalRead(buttonPinRight) == LOW)
{
Serial.write(1);
Serial.flush();
delay(20);
}
if(digitalRead(buttonPinRight) == LOW digitalRead(buttonPinLeft) == LOW)
{
Serial.write(3);
Serial.flush();
delay(20);
}
}