Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO.Ports;
  5.  
  6. public class PlayerScripts : MonoBehaviour {
  7.  
  8.     //public static PlayerScripts instance;
  9.     SerialPort spp =new SerialPort("\\\\.\\COM8",115200);
  10.     //public int a;
  11.     //public Rigidbody rigid ;
  12.     //public float speed = 1f;
  13.     public float count = 0f;
  14.     public float ro = 0.1f;
  15.     public bool turnState;
  16.     public Quaternion turnScale;
  17.     public Transform from;
  18.     public Transform left1;
  19.     public Transform left2;
  20.     public Transform left3;
  21.     public Transform left4;
  22.     public Transform left5;
  23.     public Transform right1;
  24.     public Transform right2;
  25.     public Transform right3;
  26.     public Transform right4;    
  27.     public Transform right5;
  28.     public Quaternion to;
  29.     public float smooth = 3f;
  30.     void Start()
  31.     {
  32.         //rigid = gameObject.GetComponents<Rigidbody> ();
  33.         //SerialPortScripts = SerialPortScripts.instance;
  34.         turnState = false;
  35.         print (spp.PortName.ToString ());
  36.         spp.ReadTimeout = 1;
  37.         spp.Open ();
  38.     }
  39.  
  40.     void Update()
  41.     {
  42.         if (spp.IsOpen)
  43.         {
  44.             try
  45.             {
  46.                 string ruk = spp.ReadLine() ;
  47.                 print(ruk);
  48.                 moveobj(ruk);
  49.  
  50.             }
  51.             catch (System.Exception)
  52.             {
  53.                 string z = "C0";
  54.                 moveobj(z);
  55.  
  56.                 //throw;
  57.             }
  58.         }
  59.  
  60.  
  61.     }
  62.     void moveobj(string direction)
  63.     {
  64.         if (direction == "01" )
  65.         {
  66.             count = 1;
  67.             turnState = false;
  68.             transform.Translate(Vector3.forward*ro);
  69.         }
  70.         else if (direction == "21")
  71.         {
  72.             count = 1;
  73.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-36f, 0));
  74.             turnState = true;
  75.             StartCoroutine(Turn(turnScale));
  76.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  77. //          transform.rotation = Quaternion.Lerp(from.rotation, left1.rotation, Time.deltaTime*smooth);
  78.             transform.Translate(Vector3.forward*ro);
  79.  
  80.         }
  81.         else if (direction == "41")
  82.         {
  83.             count = 1;
  84.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-72f, 0));
  85.             turnState = true;
  86.             StartCoroutine(Turn(turnScale));
  87.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  88. //          transform.rotation = Quaternion.Lerp(from.rotation, left2.rotation, Time.deltaTime*smooth);
  89.             transform.Translate(Vector3.forward*ro);
  90.         }
  91.         else if (direction == "61")
  92.         {
  93.             count = 1;
  94.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-108f, 0));
  95.             turnState = true;
  96.             StartCoroutine(Turn(turnScale));
  97.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  98. //          transform.rotation = Quaternion.Lerp(from.rotation, left3.rotation, Time.deltaTime*smooth);
  99.             transform.Translate(Vector3.forward*ro);
  100.         }
  101.         else if (direction == "81")
  102.         {
  103.             count = 1;
  104.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-144f, 0));
  105.             turnState = true;
  106.             StartCoroutine(Turn(turnScale));
  107.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  108. //          transform.rotation = Quaternion.Lerp(from.rotation, left4.rotation, Time.deltaTime*smooth);
  109.             transform.Translate(Vector3.forward*ro);
  110.         }
  111.         else if (direction == "101")
  112.         {
  113.             count = 1;
  114.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y-180f, 0));
  115.             turnState = true;
  116.             StartCoroutine(Turn(turnScale));
  117.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  118. //          transform.rotation = Quaternion.Lerp(from.rotation, left5.rotation, Time.deltaTime*smooth);
  119.             transform.Translate(Vector3.forward*ro);
  120.         }
  121.         else if (direction == "-21")
  122.         {
  123.             count = 1;
  124.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+36f, 0));
  125.             turnState = true;
  126.             StartCoroutine(Turn(turnScale));
  127.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  128. //          transform.rotation = Quaternion.Lerp(from.rotation, right1.rotation, Time.deltaTime*smooth);
  129.             transform.Translate(Vector3.forward*ro);
  130.         }
  131.         else if (direction == "-41")
  132.         {
  133.             count = 1;
  134.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+72f, 0));
  135.             turnState = true;
  136.             StartCoroutine(Turn(turnScale));
  137.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  138. //          transform.rotation = Quaternion.Lerp(from.rotation, right2.rotation, Time.deltaTime*smooth);
  139.             transform.Translate(Vector3.forward*ro);
  140.         }
  141.         else if (direction == "-61")
  142.         {
  143.             count = 1;
  144.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+108f, 0));
  145.             turnState = true;
  146.             StartCoroutine(Turn(turnScale));
  147.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  148. //          transform.rotation = Quaternion.Lerp(from.rotation, right3.rotation, Time.deltaTime*smooth);
  149.             transform.Translate(Vector3.forward*ro);
  150.         }
  151.         else if (direction == "-81")
  152.         {
  153.             count = 1;
  154.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+144f, 0));
  155.             turnState = true;
  156.             StartCoroutine(Turn(turnScale));
  157.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  158. //          transform.rotation = Quaternion.Lerp(from.rotation, right4.rotation, Time.deltaTime*smooth);
  159.             transform.Translate(Vector3.forward*ro);
  160.         }
  161.         else if (direction == "-101")
  162.         {
  163.             count = 1;
  164.             turnScale = Quaternion.Euler (new Vector3(0, from.transform.rotation.y+180f, 0));
  165.             turnState = true;
  166.             StartCoroutine(Turn(turnScale));
  167.             //transform.rotation = Quaternion.Lerp(from.rotation, to, Time.deltaTime*smooth);
  168. //          transform.rotation = Quaternion.Lerp(from.rotation, right5.rotation, Time.deltaTime*smooth);
  169.             transform.Translate(Vector3.forward*ro);
  170.         }
  171.         else
  172.         {  
  173.             if (count > 0)
  174.             {
  175.                 //print ("ruk");
  176.                 count = count - 0.015f;
  177.                 transform.Translate(Vector3.forward*count);
  178.                 //transform.position = new Vector3 (transform.position.x, transform.position.y, (float)(transform.position.z + count));
  179.             }
  180.             else
  181.                 transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z );
  182.         }
  183.     }
  184.  
  185.     public IEnumerator Turn(Quaternion turnVal){
  186.         while (turnState) {
  187.             yield return new WaitForSeconds(0f);
  188.             transform.rotation = Quaternion.Lerp(from.rotation,turnVal , Time.deltaTime);
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement