I’m working on a gesture-controlled multiplayer game, where the user controls are gesture-based and this data is sent via UDP from the controller with a camera( uses mediapipe to capture gesture data ) to Unity. I’m using photon pun2 for multiplayer functionality. And the game is on Android platform.
The problem is once I join the lobby I’m able to control my player via UDP, but once other player connects I’m not able to control player( idk whether udp is not working or problem with photon ). This issue is not there when the build is for WINDOWS.
Code:
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System;
using System.Collections.Generic;
using UnityEditor;
using Photon.Pun;
using FishNet.Connection;
using FishNet.Object;
using Unity.VisualScripting;
// Define a class to represent your data structure
[System.Serializable]
public class Coor
{
public float x;
public float y;
public float z;
}
[System.Serializable]
public class finger
{
public Coor WRIST;
public Coor THUMB_CMC;
public Coor THUMB_MCP;
public Coor THUMB_IP;
public Coor THUMB_TIP;
public Coor INDEX_FINGER_MCP;
public Coor INDEX_FINGER_PIP;
public Coor INDEX_FINGER_DIP;
public Coor INDEX_FINGER_TIP;
public Coor MIDDLE_FINGER_MCP;
public Coor MIDDLE_FINGER_PIP;
public Coor MIDDLE_FINGER_DIP;
public Coor MIDDLE_FINGER_TIP;
public Coor RING_FINGER_MCP;
public Coor RING_FINGER_PIP;
public Coor RING_FINGER_DIP;
public Coor RING_FINGER_TIP;
public Coor PINKY_MCP;
public Coor PINKY_PIP;
public Coor PINKY_DIP;
public Coor PINKY_TIP;
}
[System.Serializable]
public class MyData
{
public bool LeftHandExists;
public finger Left;
public bool RightHandExists;
public finger Right;
public bool RightThumbsUp;
public bool RightThumbsDow;
public bool RightPinch;
public bool RightShoot;
public bool RightShootClic;
public bool RightStanding;
public bool RightPeace;
public bool RightThreese;
public bool RightYo;
public bool LeftThumbsUp;
public bool LeftThumbsDown;
public bool LeftPinch;
public bool LeftShoot;
public bool LeftShootClick;
public bool LeftStanding;
public bool LeftPeace;
public bool LeftThreese;
public bool LeftYo;
public bool Nothing;
}
public class controller : MonoBehaviourPun
{
public int port;
UdpClient udpClient;
IPEndPoint remoteEndPoint;
public bool isShoot;
public bool isGrab;
public float speed = 0.01f;
public MyData dataInst;
public PhotonView view;
void Start()
{
udpClient = new UdpClient(port);
remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
//view = GetComponent<PhotonView>();
if(view == null) {
Debug.Log("problem with view");
}
// Begin receiving data asynchronously
udpClient.BeginReceive(new System.AsyncCallback(ReceiveCallback), null);
}
void ReceiveCallback(System.IAsyncResult result)
{
// Get the received data and remote endpoint
if(view.IsMine) {
byte[] receivedBytes = udpClient.EndReceive(result, ref remoteEndPoint);
string receivedString = Encoding.ASCII.GetString(receivedBytes);
string ackMessage = "ACK"; // You can adjust the ACK message as needed
byte[] ackBytes = Encoding.ASCII.GetBytes(ackMessage);
udpClient.Send(ackBytes, ackBytes.Length, remoteEndPoint);
Console.WriteLine("ACK sent.");
// Deserialize the received JSON into MyData object
dataInst = JsonUtility.FromJson<MyData>(receivedString);
// Process received data here
Debug.Log("Received: " + dataInst.LeftShoot);
// Continue listening for more data
udpClient.BeginReceive(new System.AsyncCallback(ReceiveCallback), null);
}
}
void OnDestroy()
{
// Close the UDP client when the script is destroyed
if (udpClient != null)
udpClient.Close();
}
void Update() {
if(dataInst != null && dataInst.RightShoot) {
if(view.IsMine) {
Debug.Log("Moving");
this.transform.Translate(transform.forward * speed * Time.deltaTime);
}
else{
Debug.Log("error here");
}
}
else{
isShoot = false;
isGrab = false;
Debug.Log("outer here............................");
}
}
}
I’m new to unity. Please help!