- Home /
Problem sending Rotation over bluetooth
I'm working on a game that uses bluetooth to send the players position and rotation to another device, right now only the position of the player is updated on the opponents device, Here is my code:
MAIN SCRIPT:
void SendPosition() { byte[] Input = new byte[17]; byte[] Xpos = new byte[4]; byte[] Ypos = new byte[4]; byte[] BodyRot = new byte[4]; byte[] HeadRot = new byte[4];
Xpos = NetworkManager.instance.FloatToBytes(Player.transform.position.x);
Ypos = NetworkManager.instance.FloatToBytes(Player.transform.position.y);
BodyRot = NetworkManager.instance.FloatToBytes(Player.transform.rotation.z);
HeadRot = NetworkManager.instance.FloatToBytes(PlayerHead.transform.rotation.z);
Input[0] = 0;
Input[1] = Xpos[0];
Input[2] = Xpos[1];
Input[3] = Xpos[2];
Input[4] = Xpos[3];
Input[5] = Ypos[0];
Input[6] = Ypos[1];
Input[7] = Ypos[2];
Input[8] = Ypos[3];
Input[9] = BodyRot[0];
Input[10] = BodyRot[1];
Input[11] = BodyRot[2];
Input[12] = BodyRot[3];
Input[13] = HeadRot[0];
Input[14] = HeadRot[1];
Input[15] = HeadRot[2];
Input[16] = HeadRot[3];
NetworkManager.instance.WriteMessage(Input); // message transfer
}
/////////////////////////////
// Commands from another device
/////////////////////////////
public void PutInBufferPosition(byte[] position) {
Vector2 Position;
Vector2 Rotation;
byte[] Xpos = new byte[4];
byte[] Ypos = new byte[4];
byte[] BodyRot = new byte[4];
byte[] HeadRot = new byte[4];
Xpos[0] = position[1];
Xpos[1] = position[2];
Xpos[2] = position[3];
Xpos[3] = position[4];
Ypos[0] = position[5];
Ypos[1] = position[6];
Ypos[2] = position[7];
Ypos[3] = position[8];
BodyRot[0] = position[9];
BodyRot[1] = position[10];
BodyRot[2] = position[11];
BodyRot[3] = position[12];
HeadRot[0] = position[13];
HeadRot[1] = position[14];
HeadRot[2] = position[15];
HeadRot[3] = position[16];
Position.x = NetworkManager.instance.BytesToFloat(Xpos);
Position.y = NetworkManager.instance.BytesToFloat(Ypos);
Rotation.x = NetworkManager.instance.BytesToFloat(BodyRot);
Rotation.y = NetworkManager.instance.BytesToFloat(HeadRot);
MoveEnemy(new Vector4(Position.x, Position.y, Rotation.x, Rotation.y));
}
void MoveEnemy(Vector4 Input)
{
//X = X Pos
//Y = Y Pos
//Z = Body Rot.
//W = Head Rot.
OtherPlayer.transform.position = new Vector2(Input.x, -Input.y);
OtherPlayer.transform.rotation = Quaternion.Euler(180f, 0f, Input.z);
OtherPlayerHead.transform.rotation = Quaternion.Euler(0f, 0f, Input.w);
}
Here is the Network Manager Script:
// subscription and unsubscribe to events private void OnEnable() { BluetoothForAndroid.DeviceDisconnected += ExitGameScene; BluetoothForAndroid.ReceivedByteMessage += GetMessage; } private void OnDisable() { BluetoothForAndroid.DeviceDisconnected -= ExitGameScene; BluetoothForAndroid.ReceivedByteMessage -= GetMessage; }
void Start() {
if (instance == null) instance = this; // creating singleton
}
// data transfer protocol
// you can come up with any protocol
// message[0] == 0 - change position
// message[0] == 1 - shot
// message[0] == 2 - hit
// message[0] == 3 - destroy tank
// message[0] == 4 - return to starting posion
// when a message is received, methods from the GameScene script are called
// Information about which method to call is contained in the first byte of the array
void GetMessage(byte[] message) {
switch ((int)message[0]) {
case 0:
MainScript.instance.PutInBufferPosition(message);
break;
case 1:
break;
}
}
// message transfer
public void WriteMessage(byte[] message) {
BluetoothForAndroid.WriteMessage(message);
}
// go to menu and disconnect
public void ExitGameScene() {
BluetoothForAndroid.Disconnect();
SceneManager.LoadScene("Home");
}
// converting float to byte array and back from byte array to float
public byte[] FloatToBytes(float f) {
byte[] bytes = BitConverter.GetBytes(f);
return bytes;
}
public float BytesToFloat(byte[] bytes) {
float f = BitConverter.ToSingle(bytes, 0);
return f;
}
What happens is every 30th of a sec I get the players current pos ans rot and turn it into a byte array, where I send the resulting byte array though to the network manager, whitch sends the byte array to the other device. When you recieve a byte array it is converted back into floats where it is then set as the opponents pos and rot.
Thanks in advance. EX-RAY.
Your answer
Follow this Question
Related Questions
Arduino bluetooth to android 2 Answers
Can I use the microphone in Unity without bluetooth or broadcast_sticky permissions? 0 Answers
Useing Bluetooth to link Unity and Smartphone? 1 Answer
Unity android bluetooth controller disconnect doesn't make array empty 1 Answer
Using Android plugin to access android controller information. 1 Answer