Skip to content

Instantly share code, notes, and snippets.

@ljahier
Created October 11, 2017 12:31
Show Gist options
  • Save ljahier/2e7ca9d04b534285de5e26d43366f51c to your computer and use it in GitHub Desktop.
Save ljahier/2e7ca9d04b534285de5e26d43366f51c to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class movePlayer : MonoBehaviour
{
public float speed;
private float amoutToMove;
SerialPort sp = new SerialPort("COM3", 9600);
void Start()
{
sp.Open();
sp.ReadTimeout = 1;
}
void Update()
{
amoutToMove = speed * Time.deltaTime;
if (sp.IsOpen)
{
try
{
MoveObject(sp.ReadByte());
print(sp.ReadByte());
}
catch (System.Exception)
{
throw;
}
}
}
void MoveObject(int direction)
{
if (direction == 1)
{
transform.Translate(Vector3.left * amoutToMove, Space.World);
}
if (direction == 2)
{
transform.Translate(Vector3.right * amoutToMove, Space.World);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment