Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO.Ports;
- public class PlayerScripts : MonoBehaviour {
- //public static PlayerScripts instance;
- SerialPort spp =new SerialPort("\\\\.\\COM8",115200);
- //public int a;
- //public Rigidbody rigid ;
- //public float speed = 1f;
- public float count = 0f;
- public float ro = 0.1f;
- public bool turnState;
- public Quaternion turnScale;
- public Transform from;
- public Transform left1;
- public Transform left2;
- public Transform left3;
- public Transform left4;
- public Transform left5;
- public Transform right1;
- public Transform right2;
- public Transform right3;
- public Transform right4;
- public Transform right5;
- public Quaternion to;
- public float smooth = 3f;
- void Start()
- {
- //rigid = gameObject.GetComponents<Rigidbody> ();
- //SerialPortScripts = SerialPortScripts.instance;
- turnState = false;
- print (spp.PortName.ToString ());
- spp.ReadTimeout = 1;
- spp.Open ();
- }
- void Update()
- {
- if (spp.IsOpen)
- {
- try
- {
- string ruk = spp.ReadLine() ;
- print(ruk);
- moveobj(ruk);
- }
- catch (System.Exception)
- {
- string z = "C0";
- moveobj(z);
- //throw;
- }
- }
- }
- void moveobj(string direction)
- {
- if (direction == "01" )
- {
- count = 1;
- turnState = false;
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "21")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-36f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, left1.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "41")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-72f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, left2.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "61")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-108f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, left3.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "81")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-144f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, left4.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "101")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-180f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, left5.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "-21")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+36f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, right1.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "-41")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+72f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, right2.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "-61")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+108f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, right3.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "-81")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+144f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, right4.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else if (direction == "-101")
- {
- count = 1;
- turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+180f, 0));
- turnState = true;
- StartCoroutine(Turn(turnScale));
- //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
- // transform.rotation = Quaternion.Lerp(from.rotation, right5.rotation, Time.deltaTime*smooth);
- transform.Translate(Vector3.forward*ro);
- }
- else
- {
- if (count > 0)
- {
- //print ("ruk");
- count = count - 0.015f;
- transform.Translate(Vector3.forward*count);
- //transform.position = new Vector3 (transform.position.x, transform.position.y, (float)(transform.position.z + count));
- }
- else
- transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z );
- }
- }
- public IEnumerator Turn(Quaternion turnVal){
- while (turnState) {
- yield return new WaitForSeconds(0f);
- transform.rotation = Quaternion.Lerp(from.rotation,turnVal , Time.deltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement