【问题标题】:Unity (photon/networking): how to move object that was instantiated by other player?Unity(光子/网络):如何移动由其他玩家实例化的对象?
【发布时间】: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


    【解决方案1】:

    首先,每秒有 10 条移动消息的 10 名玩家每个房间每秒生成 100 条消息。 对于实际问题 - 对于子弹,您需要做的就是实例化并为其提供初始旋转和速度。因为子弹通常不会改变它们的旋转或速度(至少在电子游戏中),你只需要这样做并且只更新每个人本地机器中的子弹。

    【讨论】:

    • 您好,我用代码示例更新了我的问题。我将如何在所有客户端上运行代码,而不仅仅是在设备子弹被实例化?
    • 接收数据也不算消息吗?我计算消息的方式是本地玩家发送 10 msg/s 并接收 90 msg/s(来自其他 9 个玩家)。这将增加 100 msg/s 但这仅适用于一名玩家。将其与玩家数量相乘,每个房间的数据总计为 1000 msg/s。
    【解决方案2】:

    你可以试试这个:

    private void OnClick_Shoot()
    {
        if (timeSinceLastBullet >= spawnTime) 
        {
            int level = 2; //it is not need for you, but you can setup some params on object
            // for example i set level for increase damage per level.
            string someMsg = "hi i'm bullet";
            object[] parametres = new object[] {
               level,
               someMsg
            }
            PhotonNetwork.Instantiate (Path.Combine ("prefabs", "Network Bullet"), transform.position, transform.rotation, 0, parametres );
    
            timeSinceLastBullet = 0f;
        } 
        else 
        {
            Debug.Log ("loading...");
        }
    }
    

    还有子弹,只需在您的预制件上添加 PhotonView 组件,它就可以工作了

    public class Bullet : MonoBehaviour {
    
       private Rigidbody2D rb;
    
       [HideInInspector]
       public float speed = 0;
       [HideInInspector]
       public Vector3 direction = Vector3.zero;
    
       void Start()
       {
           rb = GetComponent<Rigidbody2D> ();
    
           this.speed = _speed + 150f; // bullet has 150 more speed than player
           this.direction = _direction;
    
           PhotonView pView = gameObject.GetPhotonView();
           // or
           //PhotonView pView = gameObject.GetComponent<PhotonView>();
    
           damage = 10 * pView[0]; // increase damage per level
           Debug.Log(pView[1]);
    
       }
    
       void Update()
       {
           if (speed != 0) 
           {
               rb.velocity = direction * speed * Time.fixedDeltaTime;
           }
       }
    }
    

    因此,这些方法将在所有客户端本地工作。我在我的子弹上使用它。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2017-03-09
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多