Search Unity

Question parenting netcode objects all over again...

Discussion in 'Multiplayer' started by firebird721, May 12, 2023.

  1. firebird721

    firebird721

    Joined:
    Jun 8, 2022
    Posts:
    101
    ok... its a very long questkion but it took me to much time and i am a bit in despair...

    so i a read it all and got confused again and again
    i really went over all of the ngo examples - but just nothing works in my case - help is much welcome!!!

    my situation is like this

    i have a player ship

    it has a missile launcher attached(game object named origin and has a FireExpanding script - derived from firebase)

    when i shoot a laser it shuold create three parts of it - main tip and origin(all are simple gameobjects)

    originGo = initLaserParts(new GameObject("Origin"), new Vector2(0, 0), OriginImg);
    mainGo = initLaserParts(new GameObject("Main"), new Vector2(originGo.GetComponent<SpriteRenderer>().sprite.bounds.size.x * globalDirection, 0), MainImg);

    tipGo = initLaserParts(new GameObject("Tip"), new Vector2(0, 0), TipImg);


    and after i create them i need to make the missile launcher gameobject be their parent

    the problem is that when i spawn the laser object from the missile launcher and then try to spawn this three game objects and and then trying to parent them in NGO makes allot of errors and ness

    another problem is that missile launcher is spawned during the game and sometimes destroyed and then spawns again and maybe its problem - "


    i just cant make it right... please help lol...

    it should be nested like this: super laser blast is the missile launcher that should be parented by the emitter(another question - shuod i add network object to emitter during running? becuase it is destroyed/spawned during the game and not before hand)

    upload_2023-5-12_3-54-4.png

    upload_2023-5-12_3-55-28.png



    here is some scripts for some background
    _________
    public abstract class FireBase : MonoBehaviour
    {
    [Tooltip("Changes from emitter behavior (firing) to a node only (non-firing).")]
    public bool makeNodeOnly;
    [Range(-180, 180)]
    [Tooltip("Sets local rotation for this emitter.")]
    public float LocalPitch;
    [Tooltip("Sets image replacement for point indicator.")]
    public Sprite customIndicator;
    [Tooltip("Prefab shot type that this emitter fires.")]
    public GameObject Shot;
    protected SpriteRenderer rend;
    protected const float speedLimit = 100;
    [Tooltip("Sets the shot's speed when fired.")]
    [Range(.01f, speedLimit)]
    public float ShotSpeed = 30;
    [Range(-40, 40)]
    [Tooltip("Sets the local shot exit point relative to this emitter.")]
    public float LocalOffset = 0;
    [HideInInspector]
    public BasePattern controller;
    private Func<bool>[] fireMethods = new Func<bool>[4];
    protected bool AutoHold = false;
    protected Timer AutoHoldCounter = new Timer(0);
    [HideInInspector]
    public UnityEngine.Events.UnityEvent OnStoppedFiring;
    [Tooltip("Overrides the fired shot's color.")]
    public Color SpriteColor;
    protected AudioSource audiosrc;

    public virtual void Start()
    {
    controller = transform.parent.parent.GetComponent<BasePattern>();
    fireMethods[0] = ButtonPress;
    fireMethods[1] = ButtonPressAutoHold;
    fireMethods[2] = AutoFire;
    fireMethods[3] = AutoFireAutoHold;
    rend = GetComponent<SpriteRenderer>();
    audiosrc = GetComponent<AudioSource>();
    }

    public virtual void LateUpdate()
    {
    if (isNode()) return;
    if (Utilities.IsEditorMode()) return;
    bool fireCommanded = fireMethods[(int)controller.FireCommand]();
    if (fireCommanded && ShootAtCurrentInterval())
    InstantiateShot();
    if (!fireCommanded)
    if (OnStoppedFiring != null)
    OnStoppedFiring.Invoke();
    }


    _______________


    public class FireExpanding : FireBase
    {
    private GameObject shotRef;
    public override void Start()
    {
    if (Utilities.IsEditorMode())
    {
    if (!makeNodeOnly)
    checkShotMismatch(typeof(ShotBullet), "Laser");
    return;
    }
    base.Start();
    }


    public override void InstantiateShot()
    {
    GameObject firedShot = Instantiate(Shot) as GameObject;
    firedShot.transform.parent = this.transform;
    print("a " + this.transform);
    ShotBase shotScript = firedShot.GetComponent<ShotBase>();
    shotScript.ShotSpeed = ShotSpeed;
    shotScript.ExitPoint = controller.ExitPointOffset + LocalOffset;
    shotScript.Emitter = this.transform;
    shotScript.FiringScript = this;
    int physicsLayer = LayerMask.NameToLayer(rend.sortingLayerName);
    firedShot.layer = physicsLayer;

    shotScript.sortLayer = rend.sortingLayerName;
    shotScript.sortOrder = rend.sortingOrder - 9999;
    shotScript.InitialSet();
    shotRef = firedShot;
    if (audiosrc != null)
    {
    audiosrc.mute = false;
    audiosrc.loop = true;
    audiosrc.Play();

    OnStoppedFiring.AddListener(muteAudio);
    }
    }

    -----------------

    now - after i create(instanitiate) the laser i use shotLaserbase script

    it animates this parts like this:

    public abstract class ShotLaserBase : ShotBase
    {
    .....
    protected BasicAnimation originAnim;
    protected BasicAnimation tipAnim;
    protected BasicAnimation blastAnim;

    protected GameObject originGo;
    protected GameObject mainGo;
    protected GameObject tipGo;
    ...........
    public override void InitialSet()
    {
    transform.parent = Emitter;
    transform.localPosition = new Vector2(ExitPoint, 0);
    transform.rotation = Emitter.rotation;
    }
    public override void Start()
    {
    ppu = MainImg[0].pixelsPerUnit;
    collidesWith = Physics2D.GetLayerCollisionMask(gameObject.layer);
    globalDirection = (transform.parent.lossyScale.x < 0) ? -1 : 1; //needed to get the parent transform lossyScale to determine firing side

    originGo = initLaserParts(new GameObject("Origin"), new Vector2(0, 0), OriginImg);
    mainGo = initLaserParts(new GameObject("Main"), new Vector2(originGo.GetComponent<SpriteRenderer>().sprite.bounds.size.x * globalDirection, 0), MainImg);

    tipGo = initLaserParts(new GameObject("Tip"), new Vector2(0, 0), TipImg);



    originAnim = initAnimations(originGo, OriginImg);
    mainAnim = initAnimations(mainGo, MainImg);
    blastAnim = initAnimations(tipGo, BlastImg);
    tipAnim = initAnimations(tipGo, TipImg);



    }


    protected virtual void animate()
    {
    collided = false;
    originGo.SetActive(true);
    mainGo.SetActive(true);
    tipGo.SetActive(true);
    originAnim.Animate(FrameSkip);
    }