using UnityEngine; using System.Collections; using System.IO.Ports; public class test2 : MonoBehaviour { int i = 1; SerialPort stream = new SerialPort("COM3", 115200); //Set the port (com4) and the baud rate (9600, is standard on most devices) int speed = 30; int factor = 1000; float posFromSerial; void Start () { stream.Open(); //Open the Serial Stream. //StartCoroutine(Whatever() ); //InvokeRepeating ("updateserial", 2, 0.5f); } // Update is called once per frame void Update () { // if (Input.GetKeyDown ("left")) { // transform.position=rigidbody.transform.position // } //if (Input.GetKeyDown("space")) //{ // string s = i.ToString(); // stream.Write(s); //stream.BytesT; // i+=1; //} // if (Input.GetKey("space")) // { //string s = velo.ToString (); //string s = velo.ToString (); //stream.Write("-"+s); // } } void FixedUpdate(){ //rigidbody.AddForce(mouvment * speed * Time.deltaTime); //gameObject.transform.Translate (mouvment); //rigidbody.MovePosition (rigidbody.position + mouvment); //float velo = Mathf.Round (rigidbody.velocity.magnitude)+1; //string s = velo.ToString (); //stream.Write("2"); //StartCoroutine (Wait(2.0f)); //yield return new WaitForSeconds(1.0f); //StartCoroutine(Whatever()); } void OnGUI() { float speedm = Mathf.Round (rigidbody.velocity.magnitude); //float velo = Mathf.Round (rigidbody.velocity.magnitude)+1; GUI.Box(new Rect(10,10,200,90), "Measurements"); //GUI.Label(new Rect(20,40,180,20), speedm + " / " + velo + " / serial: "); float mouveHorizontal = Input.GetAxis("Horizontal"); float mouveVertical = Input.GetAxis("Vertical"); string serialR = stream.ReadLine(); posFromSerial = float.Parse (serialR); float velo = Mathf.Round (rigidbody.position.x/factor); Vector3 mouvment = new Vector3(posFromSerial/factor, 0, mouveVertical); Vector3 mouvmentrelatif = rigidbody.position - new Vector3(posFromSerial/factor, 0, mouveVertical); rigidbody.MovePosition (mouvment); } // IEnumerator Whatever() // { // float timeToWait = 10; // yield return new WaitForSeconds(timeToWait); // // // } // void updateserial(){ // float velo = Mathf.Round (rigidbody.velocity.magnitude)+1; // string s = velo.ToString (); // stream.Write("2"); // } }