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 /
avatar image
0
Question by EX-RAY · Nov 17, 2020 at 06:58 AM · androidbluetoothbytearray

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.

Comment
Add comment
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

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

304 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 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

Bluetooth serial link on Android 0 Answers

How to detect on android if bluetooth controller disconnects? 0 Answers

Input.GetKey () does not work on mobile Bluetooth keyboard 2 Answers

Send data from Android app to Windows PC Unity game 0 Answers

Android Bluetooth 0 Answers


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