Advertisement
mechagical

Coordinate Control (Unity)

Jun 25th, 2022 (edited)
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Collections.Generic;
  9.  
  10. public class CoordinateControllerScript: MonoBehaviour
  11. {
  12.     // 1. Declare Variables
  13.    
  14.     Thread receiveThread; //1
  15.     UdpClient client; //2
  16.     int port; //3
  17.     public GameObject Player; //4
  18.     float horizontalPos = 0;
  19.     float verticalPos = 0;
  20.     private float movementSpeed = 1f;
  21.  
  22.     // 2. Initialize variables
  23.     // 2. Initialize variables
  24.  
  25.     void Start ()
  26.     {
  27.         port = 5065; //1
  28.         InitUDP(); //4
  29.     }
  30.  
  31.  
  32.     // 3. InitUDP
  33.     private void InitUDP()
  34.     {
  35.         print ("UDP Initialized");
  36.         receiveThread = new Thread (new ThreadStart(ReceiveData)); //1
  37.         receiveThread.IsBackground = true; //2
  38.         receiveThread.Start(); //3
  39.     }
  40.     // 4. Receive Data
  41.     private void ReceiveData()
  42.     {
  43.         client = new UdpClient (port); //1
  44.         while (true) //2
  45.         {
  46.             try
  47.             {
  48.                 IPEndPoint anyIP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port); //3
  49.                 byte[] data = client.Receive(ref anyIP); //4
  50.                 string text = Encoding.UTF8.GetString(data); //5
  51.                 horizontalPos = ParseInput(text)[0];
  52.                 verticalPos = ParseInput(text)[1];
  53.  
  54.                 //print (">> " + horizontalPos + ", " + verticalPos);
  55.             }
  56.             catch(Exception e)
  57.             {
  58.                 print (e.ToString()); //7
  59.             }
  60.         }
  61.     }
  62.  
  63.     // 6. Check for variable value, and make the Player Jump!
  64.     void Update ()
  65.     {
  66.         Debug.Log(">> " + horizontalPos + ", " + verticalPos);
  67.         float xPos = Remap(horizontalPos, 0, 1599, -9, 9);
  68.         float yPos = Remap(verticalPos, 0, 899, 5, -5);
  69.  
  70.  
  71.         Player.transform.position = new Vector3(xPos, yPos, 0);
  72.     }
  73.  
  74.     public List<float> ParseInput(string text)
  75.     {
  76.         char[] separators = { ',', ';', '|' };
  77.         string[] strValues = text.Split(separators);
  78.  
  79.         List<float> floatValues = new List<float>();
  80.         foreach(string str in strValues)
  81.         {
  82.             float val = 0;
  83.             if (float.TryParse(str, out val))
  84.                 floatValues.Add(val);
  85.         }
  86.  
  87.         return(floatValues);
  88.     }
  89.  
  90.     public float Remap(float x, float in_min, float in_max, float out_min, float out_max) {
  91.         return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  92.     }
  93.  
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement