#Doubt when using Command

79 messages · Page 1 of 1 (latest)

river sonnet
#

I need to instantiate a projectile that is attached to the player's current equipment, the equipment is a GameObject child of the player, so I can't use the Command call by the equipment, I'm doing it in the class responsible for the attack, I tried some ways and I'm not getting. I'm leaving a part of the code the way I expect it to happen and now it asks to serialize the GameObject, I read the documentation about serialization and I didn't understand very well and how I'm going to use it, who can help I really appreciate it.

` protected virtual void RangedAttack()
{
Equipment equipment = _unitEquipment.equipment;
if (isLocalPlayer && RangedEquipment.Instance.GetType().IsInstanceOfType(equipment) && _moviment.TargetManager != null)
{
RangedEquipment rangedEquipment = (RangedEquipment)equipment;
Transform origin = GetComponent<NetworkTransformChild>().target.transform.GetChild(0).GetChild(0);
Transform targetTransform = _moviment.TargetManager.transform;
uint targetId = _moviment.TargetManager.GetComponent<NetworkIdentity>().netId;
float damage = rangedEquipment.Strenght + _attributes.Strenght;
CmdInst(rangedEquipment.Bullet.gameObject, origin, targetTransform, targetId, damage);
}
}

    [Command]
    void CmdInst(GameObject bullet, Transform origin, Transform targetTransform, uint targetID, float damage)
    {
        GameObject bulletObject = Instantiate(bullet, origin.position, origin.rotation);
        Bullet bulletScript = bulletObject.GetComponent<Bullet>();
        bulletScript.TargetPosition = targetTransform;
        bulletScript.TargetId = targetID;
        bulletScript.Damage = damage;
        NetworkServer.Spawn(bulletObject);
    }`
jaunty zodiac
#

Can just use the NetworkManager list index since the prefab needs to be in there anyway

river sonnet
#

Wow Niceeeeee

#

@jaunty zodiac i'm received this error

jaunty zodiac
river sonnet
#

hm...

jaunty zodiac
#

so you'll just need to send the player object then somehow direct to the correct transform (can't nest networked objects)

#

have it referenced in a script somewhere on the root player object

river sonnet
#

In this case, i should change my second parameter (Transform origin) for (UnitEquipment unitequipment) ? This is an example

jaunty zodiac
#

then get reference the transform in that script

river sonnet
#

Yes, it already have a reference of RightHand GameObject

#

I hope that he can gets the World position of FirePosition

river sonnet
#

Ops, ignore this, a moment

#

The FirePosition is instantiate in RunTime, when i try get it i received this error :/

#

Transform origin = unitEquipment.RightHand.transform.GetChild(0).GetChild(0); GameObject bulletObject = Instantiate(re.Bullet.gameObject, origin.position, origin.rotation);

#

If i use this way, it works, but not is the position i want:
Transform origin = unitEquipment.RightHand.transform; GameObject bulletObject = Instantiate(re.Bullet.gameObject, origin.position, origin.rotation);

jaunty zodiac
#

and then set it in the prefab in the editor inspector

#

then just use that

jaunty zodiac
#

it'll tell you which line is erroring

river sonnet
#

Ok

#

But... I FirePosition belong in other class RangedEquipment, because the transform the FirePosition changes according to the equipment

jaunty zodiac
#

guess you could reference RightHand

#

is there a script on FirePosition?

#

cus you could use GetComponentInChildren and find that script?

river sonnet
#

` public class UnitEquipment : NetworkBehaviour
{
public Equipment equipment = null;
public GameObject RightHand;
...
}

public class RangedEquipment : Equipment
{
    public GameObject FirePosition;
    public Bullet Bullet;

....
}

public class Equipment : MonoBehaviour
{...}

`

jaunty zodiac
#

then do .transform on that if it exists

#

oh, sorry thats what you tried before

#

humm

#

wonder why unitEquipment.RightHand.transform.GetChild(0).GetChild(0); didnt work

#

is RightHand being set properly?

#

Try and work out which part of that line is broken

river sonnet
#

I can have many equipments, as melee as ranged, but doesn't make sense have a propertie FirePosition in a melee

river sonnet
#

@jaunty zodiac I followed you suggest with the index the equipment and works

#

So, i do this

#

RangedEquipment re = (RangedEquipment)_unitEquipment.equipmentList[index]; GameObject bulletObject = Instantiate(re.Bullet.gameObject, re.FirePosition.transform.position, re.FirePosition.transform.rotation);

#

It's works, but doesn't gets the real WorldPosition of the FirePosition

#

The same equipment i gets the Bullet also i gets the FirePosition

jaunty zodiac
#

is that erroring now even with the .GetChilds commented out?

jaunty zodiac
#

that means RightHand is not being set, so is null and causing the error

river sonnet
#

a moment

#

@jaunty zodiac

jaunty zodiac
#

so thats working?

#

but trying to get the correct child breaks

#

weird

#

try printing to the console the origin.childCount

#

and then of origin.GetChild(0).childCount

#

and just for my sanity print origin.name, just want to check that RightHand is actually the correct gameobject

#

as i see no reason why .GetChild(0).GetChild(0) shouldnt be working

river sonnet
#

Seems that the Right Hand doesn't synchronized in the server when i change the equipment

#

@jaunty zodiac This prints to verify that there is only one player in the scene, in this prints i reconnected the player

#

I gonna try record an video

jaunty zodiac
#

thats all i can really suggest

river sonnet
#

the change Equipment i followed the tutorial from site

jaunty zodiac
#

you'll need to call ChangeEquipment

#
    [Command]
    void CmdChangeEquippedItem(EquippedItem selectedItem)
    {
        equippedItem = selectedItem;

        //This will cause local host to trigger twice!! Check for IsClient (might be on the networkmanager? cant remember) before hand:
        if(NetworkManager.IsClient) return;
        ChangeEquipment(selectedItem, selectedItem)
    }
#

this will spawn it for the server too

#

will also need to change the drop/change code to delete it

river sonnet
#

An moment, i'll verify

river sonnet
#

@jaunty zodiac Forgive me, I had to go to the doctor and I'm back now

jaunty zodiac
jaunty zodiac
river sonnet
#

Okay, i'll try

river sonnet
#

Thank you anyway