【问题标题】:Unity networking mouse positionunity 联网鼠标位置
【发布时间】:2017-06-22 22:35:20
【问题描述】:

我正在关注 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);
    }
}

我也尝试将“速度 = 方向”移动到新的项目符号脚本,但它总是执行相同的操作。

如果这是这里的解决方案,我如何使用“本地”鼠标位置?

【问题讨论】:

    标签: unity3d networking 2d


    【解决方案1】:

    如果您希望其他客户端看到客户端所看到的内容,您需要将客户端的方向、旋转和位置同步到所有其他客户端。

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

    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);
        }
    }
    

    【讨论】:

    • 感谢您的回复,从某种意义上说,每个玩家(客户端和主机)都可以控制他们开火的位置,但只能在自己的屏幕上控制,它似乎运行得更好。但是他们看到其他玩家的子弹向侧面发射(和以前一样)。我认为这可能与我在触发时遇到的错误有关:BulletSprite(Clone) (UnityEngine.GameObject) 的 SpawnObject,NetworkServer 未激活。没有活动服务器就无法生成对象。 UnityEngine.Networking.NetworkServer:Spawn(GameObject) PlayerMove:RpcFire(Vector2, Vector2, Vector3) (at Assets/Scripts/PlayerMove.cs:64)
    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多