简体   繁体   中英

How to reference instantiated object in [Command] function?

I am currently learning to make a multiplayer fps in unity but I have got a problem. I have a PlayerShoot script which handles shooting different weapons and types. Here is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Weapons;

public class PlayerShoot : NetworkBehaviour {
public WeaponManager weaponManager;

void Awake()
{
    weaponManager = GetComponent<WeaponManager> ();

    if (weaponManager == null)
        return;
}

void Update()
{
    if (!isLocalPlayer)
        return;

    // Fire
    if (Input.GetButtonDown ("Fire1")) 
    {
        HandleFire ();
    }
}

void HandleFire()
{
    Weapon currentWeapon = weaponManager.equippedWeapon;

    if (!currentWeapon.CanFire())
        return;

    switch (currentWeapon.weaponType) 
    {
        case WeaponType.THROWING:
            ThrowWeapon ((WeaponThrowing)currentWeapon);
            break;

        case WeaponType.FIREARM:
            RaycastShoot(currentWeapon);
            break;

        case WeaponType.MELEE:
            AttackMelee(currentWeapon);
            break;
    }
}

// Throwing weapon
void ThrowWeapon(WeaponThrowing weapon)
{
    GameObject throwedObject = (GameObject)Instantiate (weapon.throwObjectPrefab, weaponManager.throwWeaponPlace.position,  weaponManager.throwWeaponPlace.rotation);

    Debug.Log(throwedObject);

    Rigidbody throwedObjectRB = throwedObject.GetComponent<Rigidbody> ();

    if (throwedObjectRB != null) 
    {
        throwedObjectRB.AddForce(throwedObject.transform.forward * weapon.throwForce, ForceMode.Impulse);
    }

    CmdOnWeaponThrowed();
}

[Command]
void CmdOnWeaponThrowed()
{
    // How to access my throwed object here. 

    NetworkServer.Spawn(obj, obj.GetComponent<NetworkIdentity>().assetId);
}

// Raycast shooting
void RaycastShoot(Weapon weapon)
{

}

// Melle attack
void AttackMelee(Weapon weapon)
{

}
}

Here in the handle fire I get the equipped weapon and check its type and based on type I call method for shooting this type. In my case it is throwing the weapon. In throw weapon function I instantiate weapon prefab and then call the CmdOnWeaponThrowed to spawn the object in all clients. So my problem is I cannot access the throwedObject variable in CmdOnWeaponThrowed function cause Commands do not access objects as parameter.

错误即时通讯

Pass it to method:

[Command]
void CmdOnWeaponThrowed(GameObject obj)
{
    var netId = obj.GetComponent<NetworkIdentity>();
    if (netId == null)
    {
        // component is missing.
        return;
    }
    NetworkServer.Spawn(obj, netId.assetId);
}

then

void ThrowWeapon(WeaponThrowing weapon)
{
    GameObject throwedObject = (GameObject)Instantiate (weapon.throwObjectPrefab, weaponManager.throwWeaponPlace.position,  weaponManager.throwWeaponPlace.rotation);

    Debug.Log(throwedObject);

    Rigidbody throwedObjectRB = throwedObject.GetComponent<Rigidbody> ();

    if (throwedObjectRB != null) 
    {
        throwedObjectRB.AddForce(throwedObject.transform.forward * weapon.throwForce, ForceMode.Impulse);
    }

    CmdOnWeaponThrowed(throwedObject);
}

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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