Unity网络鼠标位置

时间:2017-06-22 22:35:21

标签: unity3d networking 2d

我正在关注Unity文档的this教程,但是希望将我的版本更改为朝向鼠标位置。主机工作正常,但在运行客户端时,子弹朝着相对于主机客户端的鼠标位置发射(通常只是偏向一侧)。

这是我的代码:

public class PlayerMove : NetworkBehaviour {

    public GameObject bulletPrefab;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (!isLocalPlayer)
            return;

        var x = Input.GetAxis("Horizontal") * 0.1f;
        var z = Input.GetAxis("Vertical") * 0.1f;

        transform.Translate(x, 0, z);

        if (Input.GetMouseButtonDown(0))
        {
            CmdFire();
        }
    }

    public override void OnStartLocalPlayer()
    {
        GetComponent<MeshRenderer>().material.color = Color.red;
    }

    [Command]
    void CmdFire()
    {
        // create the bullet object from the bullet prefab

        // make the bullet move away in front of the player
        Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
        Vector2 myPos = new Vector2(transform.position.x, transform.position.y + 1);
        Vector2 direction = target - myPos;
        GameObject projectile = (GameObject)Instantiate(bulletPrefab, myPos, transform.rotation);
        projectile.GetComponent<Rigidbody2D>().velocity = direction * 4f;

        // spawn the bullet on the clients
        NetworkServer.Spawn(projectile);

        // make bullet disappear after 2 seconds
        Destroy(projectile, 2.0f);
    }
}

我也试图移动&#34; velocity = direction&#34;一个新的子弹脚本,但它总是执行相同的。

我如何使用&#39; local&#39;鼠标位置,如果这是解决方案吗?

1 个答案:

答案 0 :(得分:0)

如果您希望他们看到客户看到的内容,您需要在所有其他客户端上同步客户端的方向,轮换和位置。

此外 - [Command]仅在服务器上运行,您可能希望在连接到服务器的所有客户端上生成项目符号,因此您应该从[ClientRpc]运行[Command]

public class PlayerMove : NetworkBehaviour {

    public GameObject bulletPrefab;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (!isLocalPlayer)
            return;

        var x = Input.GetAxis("Horizontal") * 0.1f;
        var z = Input.GetAxis("Vertical") * 0.1f;

        transform.Translate(x, 0, z);

        if (Input.GetMouseButtonDown(0))
        {
            Fire();
        }
    }

    public override void OnStartLocalPlayer()
    {
        GetComponent<MeshRenderer>().material.color = Color.red;
    }

    void Fire()
    {
        Vector2 _target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
        Vector2 _myPos = new Vector2(transform.position.x, transform.position.y + 1);
        Vector2 _direction = _target - _myPos;
        Vector3 _rot = transform.rotation.eulerAngles;

        CmdFire(_myPos, _myDir, _rot);
    }

    [Command]
    void CmdFire(Vector2 _myPos, Vector2 _myDir, Vector3 _rot)
    {
        RpcFire(_myPos, _myDir, _rot);
    }

    [ClientRpc]
    void RpcFire(Vector2 _myPos, Vector2 _myDir, Vector3 _rot)
    {
         // create the bullet object from the bullet prefab

        // make the bullet move away in front of the player

        GameObject _projectile = (GameObject)Instantiate(bulletPrefab, _myPos, Quaternion.Euler(_rot);
        _projectile.GetComponent<Rigidbody2D>().velocity = direction * 4f;

        // spawn the bullet on the clients
        NetworkServer.Spawn(_projectile);

        // make bullet disappear after 2 seconds
        Destroy(_projectile, 2.0f);
    }
}