Search Unity

Shooting projectile over network (RPC Update)

Discussion in 'Scripting' started by Gogin, Aug 22, 2018.

  1. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    Hi,

    I am making TD game and I am facing network issue in shooting projectile over network. Object Instantiate game object called "Projectile" and gives target and source location. Target gameobject has NetworkTransform and NetworkIdentity, projectile has same as well (NetworkIdentity, NetworkTransform). Once projectile trigger target collider, it stick to it (this.transform.parent = _target.transform). Then its removed some components which are not needed any more (I guess here is the issue or in RPC function),

    I am not sure if its good to use function RpcMoveTo to update clients this way (if I am not using this, its laggy as hell). Please advise right way how to do it properly. Thank you!

    Additonal questions:
    1. Can child object have Network Identity when parent object already have Network Identity?
    2. How to properly sync moving object though network client x server? Should I use thic RPC function called RpcMoveTo?
    3. How to stick projectile to parent on network?

    please see code below, for this code I am getting error:

    HandleTransform null target
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

    Code (CSharp):
    1.     [ClientRpc]
    2.     public void RpcMoveTo(GameObject obj, Vector3 newPosition, Quaternion newQuaternion)
    3.     {
    4.         if (obj != null && obj.activeInHierarchy)
    5.         {
    6.             obj.transform.position = newPosition; //this will run in all clients
    7.             obj.transform.rotation = newQuaternion;
    8.         }
    9.     }
    10.  
    11.   void Update()
    12.     {
    13.  
    14.         //Runs only fon server
    15.         if (!isServer)
    16.         {
    17.             return;
    18.         }
    19.  
    20.         //isnt target already dead and round is running
    21.         if (_target != null && _target.gameObject.activeInHierarchy && controler.isRoundRunning)
    22.         {
    23.  
    24.             //Target hit
    25.             if (isTargetHit)
    26.             {
    27.                 mobTarget.GenerateBlood(_target.gameObject, 3, 6, -1, 1, 0.5f, 1.5f, mobTarget.heightPlayingGround);
    28.  
    29.                 mobTarget.ApplyDamage(_target.gameObject, playersWhoOwnObject, minusHp);
    30.  
    31.                 this.transform.parent = _target.transform;
    32.  
    33.                 Destroy(this.gameObject.GetComponent<Rigidbody>()); //destroy, no more needed
    34.                 Destroy(this.gameObject.GetComponent<BoxCollider>()); //destroy, no more needed
    35.                 Destroy(this.gameObject.GetComponent<Projectile>()); //destroy script, no more needed
    36.  
    37.                 //TODO: repair errors
    38.                 Destroy(this.gameObject.GetComponent<NetworkTransform>()); //destroy, no more needed
    39.                 //this.gameObject.AddComponent<NetworkTransformChild>();
    40.                 //Destroy(this.gameObject.GetComponent<NetworkIdentity>()); //destroy, no more needed
    41.             }
    42.             else
    43.             {
    44.                 //TODO: When its hit go there!
    45.                 //when mob isnt fighting chase range mob who is hitting him!
    46.                 if (!mobTarget.fighting)
    47.                 {
    48.  
    49.                     mobTarget.chaseRange = 50;
    50.                 }
    51.  
    52.                 transform.Translate(Vector3.forward * Time.deltaTime * projectileSpeed);
    53.                 transform.LookAt(targetRandomPosition());
    54.  
    55.                 //TODO: Debug!
    56.                 RpcMoveTo(this.gameObject, this.transform.position, this.transform.rotation); //Send information to clients
    57.             }
    58.  
    59.         }
    60.         else
    61.         {
    62.             //targer already dead - destroy projectile
    63.             //NetworkServer.Destroy(this.gameObject);
    64.             Destroy(this.gameObject);
    65.         }
    66.     }
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    1) Movement - Okay I'm far from a master at this, but I think you need prediction points to relay the position/rotation information. I'm assuming the projectile is moving in a parabolic way using velocity. What I'd likely suggest is the server passes the position, rotation, and velocity. From there the local client iterates the velocity on physics updates using client side gravity/mass. The client will take network position/velocity updates periodically from there, and treat them as corrections to their client side physics solving.

    2) Sticking and Parenting - You can't parent Networked Objects to another Networked object. What you can do is Instantiate a Transform which is a child of what you want it to be a parent of, and write a follow script that will update it's position per frame to make sure it keeps it's relative position to that Transform. Just an idea, and will only be optimal for certain projects. You could also just create a separate spawnable object, which is the 'hit effect', and give that it's own system. These are just ideas. Hopefully someone will chime in with some alternatives too.
     
  3. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    Thank you fast reply, can you please give me some code to understand your ideas better? I am rewriting old code to network so I am not so skilled in this area. Thank you!