Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AddExplosionForce to Client

Discussion in 'Multiplayer' started by Zantonius, Aug 15, 2018.

  1. Zantonius

    Zantonius

    Joined:
    Jul 3, 2018
    Posts:
    1
    Hello,

    I am playing around with Unet and Unity and I am casting a fireball. The "fireball" is supposed to create a knockback when it hits a player. The player who is both the server and the client is being knocked back correctly when the fireball hits them.

    However, the player who is only the client simply "hops" a short distance very quickly and then very quickly returns to the original spot before the fireball hits. The length of the "hop" depends on the force in the "AddExplosionForce" method, so it does somehow function a little bit.

    Below are my methods.

    Code (CSharp):
    1. [Command]
    2.     void CmdCastFireball(Vector3 aimPosition)
    3.     {
    4.         Vector3 yOffset = new Vector3(0, 0.5f, 0); // Makes the fireball not go at floor level
    5.         GameObject fireballInstance = Instantiate(fireballPrefab, transform.position + yOffset, transform.rotation);
    6.  
    7.         fireballInstance.GetComponent<Fireball>().sourceSlinger = this;
    8.         Rigidbody rb = fireballInstance.GetComponent<Rigidbody>();
    9.         rb.velocity = fireballSpeed * aimPosition.normalized;
    10.  
    11.         NetworkServer.Spawn(fireballInstance);
    12.     }
    Code (CSharp):
    1. void OnTriggerEnter(Collider collider)
    2.     {
    3.         if (isServer == false)
    4.         {
    5.             Debug.Log("OnTriggerEnter made my client");
    6.             // Only the server resolves explosions
    7.             return;
    8.         }
    9.  
    10.         // Is this our Spellslinger? Don't hit it please
    11.         if (sourceSlinger.GetComponent<Rigidbody>() == collider.attachedRigidbody)
    12.         {
    13.             return;
    14.         }
    15.  
    16.         Collider[] cols = Physics.OverlapSphere(this.transform.position, radius, slingerMask);
    17.      
    18.         foreach (Collider col in cols)
    19.         {
    20.             //Debug.Log(col.gameObject.name);
    21.             if (col.attachedRigidbody == null)
    22.             {
    23.                 continue;
    24.             }
    25.  
    26.             targetRigidbody = col.attachedRigidbody;
    27.          
    28.             if (!targetRigidbody)
    29.                 continue;
    30.  
    31.             RpcAddForce();
    32.         }
    33.     }
    Code (CSharp):
    1. [ClientRpc]
    2.     void RpcAddForce()
    3.     {
    4.         targetRigidbody.freezeRotation = true;
    5.         targetRigidbody.AddExplosionForce(700f, this.transform.position, radius);
    6.         targetRigidbody.freezeRotation = false;
    7.         Destroy(gameObject);
    8.     }
    I have checked the distance to the client player when the fireball hits, and it is the same distance as when the fireball hits the server/client player. Can someone please point out what I'm missing? Or is the AddExplosionForce-method simply not viable for networked games?

    Thanks in advance,
    Niklas