Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_471BC89795A9820FA088 · Oct 31, 2021 at 01:33 AM · webrequestarduino

Joystick Controller using Unity Webrequest

Hi there,

I'm trying to get a Joystick data values, map them and send them to a web server using NodeMCU. Get the data from the web server and use it in a Unity Game to control a Player's movement.

The problem: The response time from Joystick to the Player movement is VERY SLOW, like 1 second delay or more.

I'm trying to make it so that the response time is acceptable, and still stick to the main ideea: using a web server to communicate with the Unity Game.

If there is a better ideea to my approach in the code, please lend me a hand.

Here is the Unity Code, there must be something that I do wrong to get those delays:

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 using System;
 public class joycontroller : MonoBehaviour
 {
     string currentWebString, lastWebString = "2";
     public CharacterController controller;
     float horizontal = 0f, vertical= 0f;
     public float speed = 15f;
     public float gravity = -19.62f;
     public float jumpHeight = 2f;
     public Transform groundCheck;
     public float groundDistance = 0.3f;
     public LayerMask groundMask;
     Vector3 velocity;
     bool isGrounded, jump;
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         StartCoroutine(GetRequest("http://192.168.0.102/"));
     }
     void Update()
     {
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
         if(isGrounded && velocity.y < 0f)
         {
             velocity.y = -2f;
         }
         Vector3 move = transform.right * horizontal + transform.forward * vertical;
         controller.Move(move * speed * Time.deltaTime);
         if(jump && isGrounded)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
             jump = false;
         }
         velocity.y += gravity * Time.deltaTime;
         controller.Move(velocity * Time.deltaTime);
     }
 
     void moveCommander(string commandString)
     {
         char[] spearator = { ';'};
         Int32 count = 3;
         String[] tempString = commandString.Split(spearator, count, StringSplitOptions.None);
         int[] commandArray = Array.ConvertAll(tempString, int.Parse);
         if(commandArray[0] == 1) jump = true;
         else jump = false;
         horizontal = commandArray[1];
         vertical = commandArray[2];
         Debug.Log("Button: " + commandArray[0] + " X: " + commandArray[1] + " Y: " + commandArray[2]);
     }
 
     IEnumerator GetRequest(string uri)
     {
         while (true)
         {
             UnityWebRequest uwr = UnityWebRequest.Get(uri);
             yield return uwr.SendWebRequest();
             
             if (uwr.isNetworkError)
             {
                 Debug.Log("Error While Sending: " + uwr.error);
             }
             else
             {
                 currentWebString = uwr.downloadHandler.text;
                 if(!String.Equals(currentWebString, lastWebString)) moveCommander(currentWebString);
                 lastWebString = String.Copy(currentWebString);
             }
             yield return new WaitForSeconds(0.1f);
         }
     }
 }

And just in case, here is the NodeMCU code:

 #include <ESP8266WiFi.h>
 #include <ESP8266WebServer.h>
 const char* ssid = "xxx";
 const char* password = "xxx";
 ESP8266WebServer server(80);
     #define S0  D0
     #define S1  D1
     #define S2  D2
     #define S3  D3
     #define SIG A0
     #define BP  D7
     int sensor13;
     int sensor14;
     int sensor15;
     char commandArray[16];
     char temp[8];
 void setup() {
     pinMode(S0,OUTPUT);      
     pinMode(S1,OUTPUT); 
     pinMode(S2,OUTPUT);
     pinMode(S3,OUTPUT);
     pinMode(SIG, INPUT); 
     pinMode(BP, INPUT);
     Serial.begin(9600);
     delay(200);
     Serial.println("Connecting to ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) 
     {
       delay(1000);
       Serial.print(".");
     }
     Serial.println("");
     Serial.println("WiFi connected..!");
     Serial.print("Got IP: ");  
     Serial.println(WiFi.localIP());
     
     server.on("/", serverHandler);
     server.begin();
     Serial.println("HTTP server started");
 }
 void loop() {
     server.handleClient();
     sensor13 = digitalRead(BP);
     // Channel 14 (C14 pin - binary output 0,1,1,1)
     digitalWrite(S0,LOW); digitalWrite(S1,HIGH); digitalWrite(S2,HIGH); digitalWrite(S3,HIGH);
     sensor14 = analogRead(SIG);
     // Channel 15 (C15 pin - binary output 1,1,1,1)
     digitalWrite(S0,HIGH); digitalWrite(S1,HIGH); digitalWrite(S2,HIGH); digitalWrite(S3,HIGH);
     sensor15 = analogRead(SIG);
   
     Serial.print("X : ");Serial.print(mapCoordinate(sensor14));
     Serial.print("    Y : ");Serial.print(-mapCoordinate(sensor15));
     Serial.print("    B : ");Serial.println(sensor13);
     commandString(sensor13, sensor14, sensor15);
     delay(500);
 }
 void serverHandler() {
   server.send(200, "text/html", commandArray);
 }
 void commandString(unsigned int sw,unsigned int x,unsigned int y){
   itoa(sw, temp, 10);
   strcpy(commandArray, temp); strcat(commandArray, ";");
   itoa(mapCoordinate(x), temp, 10);
   strcat(commandArray, temp); strcat(commandArray, ";");
   itoa(-mapCoordinate(y), temp, 10);
   strcat(commandArray, temp); 
 }
 int mapCoordinate(int coordinate){
   if(coordinate >= 0 && coordinate <= 255) return -1;
   else if(coordinate >= 256 && coordinate <= 767) return 0;
   else if(coordinate >= 768 && coordinate <= 1024) return 1;
 }

And even a video with the delay: https://youtu.be/xttRqo-II8E

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rh_galaxy · Oct 31, 2021 at 11:05 PM 0
Share

Sending movement to a webserver and wait for response before moving player will almost certainly give an unacceptable delay... even 200ms will be too much. Especially when you stop your input and the player keeps going for a while is extra noticeable.

One thing you can do is move directly and undo move which was not allowed when you get the response... but it is not clear to me why you want to do it this like this. There will be delays.

avatar image unity_471BC89795A9820FA088 rh_galaxy · Nov 01, 2021 at 05:55 PM 0
Share

I just want to control the game player, wireless, like either using a web server or other method of data transmission like bluetooth. But as I'm new to this I'm not sure which one is really the best. Would I still get so huge delays even with via bluetooth? Or there is really no way to get rid of the delays using a web server? Thank you for replying :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connect Unity to Microsoft azure database? 4 Answers

How to replace WWW() with UnityWebRequest 1 Answer

What are the best practices to download, store and display 2D and 3D resources in an AR environment? 0 Answers

using arduino input in mobile 0 Answers

UnityWebRequest.downloadProgress keep coming back 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges