【发布时间】:2017-11-29 17:06:52
【问题描述】:
让我的 Photon 项目低于给定的 500 msg/s 真的很棘手。即使房间里有 10 个玩家,每个更新位置每秒 10 次 10(玩家)* 10(发送的消息)* 10(接收的消息)= 1000 消息/秒。那只是球员的运动。接下来我需要移动子弹,这将再次增加消息量。
目前我已经通过网络实例化了子弹,但只有本地玩家能够移动它,因为我还没有同步子弹的移动。我想知道一旦实例化而不是通过网络传递位置,我是否可以让所有客户端开始在他们的本地设备上移动子弹?这将节省大量消息,因为我永远不必通过网络发送子弹位置。
黑客和作弊在我的游戏中不是问题。
编辑:这是我目前用来移动子弹的脚本。这仅在实例化子弹的设备上本地工作。如何在每个客户端本地运行此脚本?
public class Network_Bullet : Photon.MonoBehaviour {
private Rigidbody2D rb;
[HideInInspector]
public float speed = 0;
[HideInInspector]
public Vector2 direction = Vector2.zero;
public void SetValues(float _speed, Vector2 _direction)
{
rb = GetComponent<Rigidbody2D> ();
this.speed = _speed + 150f; // bullet has 150 more speed than player
this.direction = _direction;
}
private void Update()
{
if (speed != 0)
{
rb.velocity = direction * speed * Time.fixedDeltaTime;
}
}
}
这里是子弹的实例化方法:
private void OnClick_Shoot()
{
if (photonView.isMine == true)
{
if (timeSinceLastBullet >= spawnTime)
{
GameObject newBullet = PhotonNetwork.Instantiate (Path.Combine ("prefabs", "Network Bullet"), transform.position, transform.rotation, 0);
newBullet.GetComponent<Network_Bullet> ().SetValues (owner.speed, new Vector2(owner.last_horizontal, owner.last_vertical));
timeSinceLastBullet = 0f;
}
else
{
Debug.Log ("loading...");
}
}
}
【问题讨论】:
标签: unity3d