Hey, I am attempting to implement a UDP client / server which simply updates the rigidbody position whenever it recieves a package.
Below is my current implementation that attempts to do it asynchronously but for some reason the Playermovement class fails silently on the rb.position call below, it just does nothing (no errors either but the client.BeginRecieve does not reexecute which means that it’s stuck somehow). It seems to fail using anything from Monobehaviour and I am wondering why this happens and if there is something I can do to get this to work?
If I remove the rb.position and update it in the fixedUpdate instead everything works as intended but I would rather update it on a UDP package rather than every frame hence my question.
public abstract class NetworkMonoBehaviour<T> : MonoBehaviour {
private void ReceiveCallback(IAsyncResult result)
{
byte[] receivedBytes = client.EndReceive(result, ref remoteEndPoint);
string receivedString = Encoding.ASCII.GetString(receivedBytes);
NetworkPackage<T> receivedPackage = JsonUtility.FromJson<NetworkPackage<T>>(receivedString);
if (receivedPackage.uuid == uuid)
{
DataReceived(receivedPackage.data);
}
client.BeginReceive(AC, client);
}
protected abstract void DataReceived(T data);
}
PlayerMovement : NetworkMonoBehaviour<networkpackagetype>
{
// rb = gameObject.GetComponent<Rigidbody2D>();
protected override void DataReceived(MovementNetworkPackage data)
{
Debug.Log("Rigid body");
Vector2 _dir = new Vector2(data.direction_x, data.direction_y);
Vector2 _pos = new Vector2(data.position_x, data.position_y);
/* This code fails silently with nothing happening, if I update the dir and position in fixed update it works */
rb.position = _pos + _dir;
}
}