简体   繁体   English

如何在设备之间实时同步Unity物理? (适用于Unity的Google Play游戏插件)

[英]How to synchronize Unity physics between devices in real time? (Google Play Games plugin for Unity)

I'm working on real-time multiplayer game in Unity. 我正在Unity中进行实时多人游戏。 Connection between two players is established with Google Play Game Services. 两个玩家之间的联系是通过Google Play游戏服务建立的。
Simple example of action - shooting from a slingshot: player pulls rubber with his finger and makes shot. 一个简单的动作示例-从弹弓射击:玩家用手指拉橡胶并射击。 The only important initial data is position of rubber before player will release it, so its coordinates are sent on other device. 唯一重要的初始数据是玩家释放橡胶之前的橡胶位置,因此其坐标将在其他设备上发送。 After this projectile gets velocity ( RigidBody2D.velocity = new Vector2(...) ) and flies. 此弹丸获得速度后( RigidBody2D.velocity = new Vector2(...) )飞翔。

Problem is having the same initial data and executing the same code projectile can hit the target on one device and miss on other. 问题在于,具有相同的初始数据并且执行相同的代码弹丸会击中一台设备上的目标而错过另一台设备上的目标。

Unity's physics engine is not finite state engine and may simulate differently in different devices. Unity的物理引擎不是有限状态引擎,并且在不同设备中的模拟可能不同。 But, still I used this in a simple manner for a physics based 2d game and worked out pretty well. 但是,我仍然以简单的方式将其用于基于物理学的2d游戏,并且效果很好。 Here's the steps that I followed: 这是我遵循的步骤:
1. Before each move, I made sure every physics objects are in same place over the networked devices. 1.在进行每一步之前,我确保每个物理对象都位于网络设备上的同一位置。
2. Added force to the player object in the local player. 2.向本地播放器中的播放器对象添加了力量。 Then sent the force position and direction over network to other devices. 然后将力的位置和方向通过网络发送给其他设备。 Then simulated on other devices according to it. 然后根据它在其他设备上进行仿真。
3. After the move is complete(on other words, all the rigidbodies are stopped), double checked if the positions, rotations are same on all devices. 3.移动完成后(换句话说,所有刚体都停止了),再次检查所有设备上的位置和旋转是否相同。 If not, aligned them according to the local player who made the move. 如果没有,请根据进行此操作的本地玩家调整他们。

Repeated the above steps. 重复以上步骤。 The 3rd step is just to be 100% sure that all objects are in sync in all the devices, before I take the next move. 第三步是在我采取下一步行动之前,确保100%确保所有设备中的所有对象都是同步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM