Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Collections.Generic;
- public class CoordinateControllerScript: MonoBehaviour
- {
- // 1. Declare Variables
- Thread receiveThread; //1
- UdpClient client; //2
- int port; //3
- public GameObject Player; //4
- float horizontalPos = 0;
- float verticalPos = 0;
- private float movementSpeed = 1f;
- // 2. Initialize variables
- // 2. Initialize variables
- void Start ()
- {
- port = 5065; //1
- InitUDP(); //4
- }
- // 3. InitUDP
- private void InitUDP()
- {
- print ("UDP Initialized");
- receiveThread = new Thread (new ThreadStart(ReceiveData)); //1
- receiveThread.IsBackground = true; //2
- receiveThread.Start(); //3
- }
- // 4. Receive Data
- private void ReceiveData()
- {
- client = new UdpClient (port); //1
- while (true) //2
- {
- try
- {
- IPEndPoint anyIP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port); //3
- byte[] data = client.Receive(ref anyIP); //4
- string text = Encoding.UTF8.GetString(data); //5
- horizontalPos = ParseInput(text)[0];
- verticalPos = ParseInput(text)[1];
- //print (">> " + horizontalPos + ", " + verticalPos);
- }
- catch(Exception e)
- {
- print (e.ToString()); //7
- }
- }
- }
- // 6. Check for variable value, and make the Player Jump!
- void Update ()
- {
- Debug.Log(">> " + horizontalPos + ", " + verticalPos);
- float xPos = Remap(horizontalPos, 0, 1599, -9, 9);
- float yPos = Remap(verticalPos, 0, 899, 5, -5);
- Player.transform.position = new Vector3(xPos, yPos, 0);
- }
- public List<float> ParseInput(string text)
- {
- char[] separators = { ',', ';', '|' };
- string[] strValues = text.Split(separators);
- List<float> floatValues = new List<float>();
- foreach(string str in strValues)
- {
- float val = 0;
- if (float.TryParse(str, out val))
- floatValues.Add(val);
- }
- return(floatValues);
- }
- public float Remap(float x, float in_min, float in_max, float out_min, float out_max) {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement