Search Unity

Non-Player Obj - Different Behavior between Server and Client

Discussion in 'Multiplayer' started by BahamutZeRo, May 25, 2016.

  1. BahamutZeRo

    BahamutZeRo

    Joined:
    Oct 19, 2015
    Posts:
    17
    This #C script attached to a Non Player Obj name "SelfDestruct_Creep". The purpose of this script is trying to achieve "Approach Any Player within a Range".

    In this Script contains key functions of:-
    - OnTriggerEnter behaviour to get object in Range.
    - Only "Player" Tag will get to store in a variable called "TargetedPlayer".
    - Order this Non Player Object(SelfDestruct_Creep) to move towards TargetedPlayer.

    Current Issues I'm now facing is:
    - Getting any "Player" object in range its working, but when checking on both of my Server and Client the so called "Targeting Player" function. SelfDestruct_Creep seems to target the different Player on both Server and Client.

    Example Scenario:
    - [In Scene]one SelfDestruct_Creep and two Players(Player 1 and Player 2).
    - [Server] SelfDestruct_Creep approach Player 1.
    - [Client] SelfDestruct_Creep approach Player 2.
    Above scenario happens at the same timing. But SelfDestruct_Creep approaches different Player which causes both player DEAD instead of the one.

    For what I can think of now is that, I need to tell the client from the server this SelfDestruct_Creep is targeting "Example_Player". But I do not know how to achieve that on an "Non-Player Object". So if any of you understand my problem, please do enlighten me. I'll be appreciate it! Thank you gent! :)

    Below is the script I have mentioned:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class ExampleScript : NetworkBehaviour
    6. {
    7.     public float Speed = 5f;
    8.     public GameObject TargetedPlayer;
    9.  
    10.     void Update()
    11.     {
    12.         if(TargetedPlayer!=null)
    13.         {
    14.             Movement(TargetedPlayer);
    15.         }
    16.         else
    17.         {
    18.             Destroy(this.gameObject);
    19.         }
    20.     }
    21.      
    22.     public void Movement(GameObject _TargetedPlayer)
    23.     {
    24.         transform.LookAt(_TargetedPlayer.transform.position);
    25.         transform.Rotate(new Vector3(0,-90,0),Space.Self);
    26.         if (Vector3.Distance(transform.position,_TargetedPlayer.transform.position)> 0f)
    27.         {
    28.             transform.Translate(new Vector3(Speed*2* Time.deltaTime,0,0));
    29.         }
    30.     }
    31.      
    32.     void OnTriggerEnter2D (Collider2D col)
    33.     {
    34.         if(col.gameObject.tag == "Player")
    35.         {
    36.             TargetedPlayer = col.gameObject;
    37.         }
    38.     }
    39. }
     
    Last edited: May 25, 2016
  2. JirinCrawford

    JirinCrawford

    Joined:
    May 2, 2016
    Posts:
    5
    Assuming your players have NetworkIdentities, you probably want to add a SyncVar holding the target's netId. You can add a hook function to the SyncVar to update TargetedPlayer on the client whenever the SyncVar changes on the server. Then you just need to make sure only the server changes the SyncVar and TargetedPlayer.

    SyncVars are explained at http://docs.unity3d.com/Manual/UNetStateSync.html.
     
    BahamutZeRo likes this.
  3. BahamutZeRo

    BahamutZeRo

    Joined:
    Oct 19, 2015
    Posts:
    17
    @JirinCrawford Hey budds! Thanks for your reply. From your suggested solution, I have derived my latest codes.

    [Applied in Script]
    - Using networkIdentitties to get the NetId is working.
    - Search for the Player' GameObject using NetId is working. However its only On the server, but not the client...again :(

    I'm using SyncVar Hook but it does not sync to the client. May I know why...? :)
    This Non-Player-Obj really kills me :(

    Below is the latest code Im working on. If you understand what Im missing please enlighten me :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class ExampleScriptForSelfDestructCreep : NetworkBehaviour
    6. {
    7.     public float Speed = 5f;
    8.     public GameObject TargetedPlayer;
    9.     [SyncVar(hook = "Movement")]
    10.     private NetworkInstanceId TargetedId;
    11.  
    12.     void Update()
    13.     {
    14.         if(TargetedPlayer!=null)
    15.         {
    16.             if(TargetedId != null)
    17.             {
    18.                 Movement(TargetedId);  
    19.             }
    20.         }
    21.         else
    22.         {
    23.             Destroy(this.gameObject);
    24.         }
    25.     }
    26.  
    27.  
    28.     public void Movement(NetworkInstanceId _TargetedId)
    29.     {
    30.         GameObject _TargetedPlayer = NetworkServer.FindLocalObject(_TargetedId); // Suspect this function on invoke on server not on client?
    31.         Debug.Log("Targeted Player =" + _TargetedPlayer);
    32.         transform.LookAt(_TargetedPlayer.transform.position);
    33.         transform.Rotate(new Vector3(0,-90,0),Space.Self);
    34.         if (Vector3.Distance(transform.position,_TargetedPlayer.transform.position)> 0f)
    35.         {
    36.             transform.Translate(new Vector3(Speed*2* Time.deltaTime,0,0));
    37.         }  
    38.     }
    39.  
    40.     void OnTriggerEnter2D (Collider2D col)
    41.     {
    42.         if(col.gameObject.tag == "Player")
    43.         {
    44.             TargetedPlayer = col.gameObject;
    45.             TargetedId = TargetedPlayer.GetComponent<NetworkIdentity>().netId;
    46.  
    47.             Movement(TargetedId);
    48.         }
    49.     }
    50. }
     
  4. JirinCrawford

    JirinCrawford

    Joined:
    May 2, 2016
    Posts:
    5
    You're on the right track. As you've noticed, NetworkServer.FindLocalObject doesn't work on clients. You need to use ClientScene.FindLocalObject instead.

    At the moment you're calling Movement when your SyncVar changes. You'd probably be better off creating a different function which just finds the player and stores it in TargetedPlayer instead (something like OnTargetedIdChange) to use as your hook function. That way you'd only need to look up TargetedPlayer once each time TargetedID changes, instead of every time you move.

    OnTriggerEnter2D can happen on both the client and the server at the moment, but you probably want it to only happen on the server (otherwise the client could overwrite values it gets from the server). You can do this by checking that this.isServer is true at the start of OnTriggerEnter2D before doing anything.

    Hope that helps!
     
    BahamutZeRo likes this.
  5. BahamutZeRo

    BahamutZeRo

    Joined:
    Oct 19, 2015
    Posts:
    17
    @JirinCrawford I followed your solutions, which I think might reasonable to make it work but eventually I still missing some minor thing. Let me break it down.

    [Applied in Script]
    - Split Movement Function and GetTarget Function.
    - SyncVar Hook on NetworkInstanceId to GetTarget Function.
    - OnTriggerEnter2D will only happen on Server. (This cause client doesn't get this information but I learnt that we can use Hook, but I dont understand why it doesn't work?)

    [Things I've Tried]
    - Tried to call synchronization on "void Start". (Failed)
    - Changing Network Identity of Selfdestruct to "Server Only" from "Local Player Auth". (Currently remain untick for both option). (Failed)

    Current issue is that I still couldn't manage to send information like " i.e. Telling Client that this (Non-PlayerObj)Selfdestruct Creep is chasing Player 1 only." Which I checked from client that, the client is unable to spot this Selfdestruct Creep is making any movement, because it does not have TargetedPlayer and TargetedId(which im still trying to Sync from Server to client). I think it could be related to making this NonPlayerObj as Server Only? Let me know what do you think :). Or if any viewer understand my issue please do enlighten me. Appreciate your time with me;)

    Below is my latest Script(still keep trying!! Its worth it!!)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Example_SelfDestructCreep : NetworkBehaviour
    6. {
    7.     #region Variables
    8.     public float Speed = 5f;
    9.  
    10.     public GameObject TargetedPlayer;
    11.  
    12.     [SyncVar(hook = "GetTarget")] // Somehow this hook doesnt send to other client when this SelfDestruct_Creep got it's target.
    13.     public NetworkInstanceId TargetedId;
    14.     #endregion
    15.  
    16.     void Update()
    17.     {
    18.         if(TargetedPlayer!=null)
    19.         {
    20.             Movement(TargetedPlayer);
    21.         }
    22.         else
    23.         {
    24.             return;
    25.         }
    26.     }
    27.  
    28.     void GetTarget(NetworkInstanceId _TargetedId) // Getting Targeted Player
    29.     {
    30.         GameObject _TargetedPlayer = ClientScene.FindLocalObject(_TargetedId);
    31.  
    32.         Movement(_TargetedPlayer);
    33.     }
    34.  
    35.     public void Movement(GameObject _TargetedPlayer) // Movement
    36.     {
    37.         transform.LookAt(_TargetedPlayer.transform.position);
    38.         transform.Rotate(new Vector3(0,-90,0),Space.Self);
    39.         if (Vector3.Distance(transform.position,_TargetedPlayer.transform.position)> 0f)
    40.         {
    41.             transform.Translate(new Vector3(Speed*2* Time.deltaTime,0,0));
    42.         }  
    43.     }
    44.  
    45.     void OnTriggerEnter2D (Collider2D col)
    46.     {
    47.         if(isServer) // Passing Details only on Server but Not Client
    48.         {
    49.             if(col.gameObject.tag == "Player")
    50.             {
    51.                 Debug.LogError("Got a Player");
    52.                 TargetedPlayer = col.gameObject;
    53.  
    54.                 TargetedId = TargetedPlayer.GetComponent<NetworkIdentity>().netId;
    55.  
    56.                 GetTarget(TargetedId);
    57.             }      
    58.         }
    59.     }
    60. }
     
    Last edited: May 27, 2016
  6. BahamutZeRo

    BahamutZeRo

    Joined:
    Oct 19, 2015
    Posts:
    17
    [SOLVED] - Solution
    - Replace _TargetedPlayer with TargetedPlayer.
    - Remove TargetedPlayer's line under void OnTriggerEnter2D.
    [Reason]
    - If TargetedPlayer variable assign under OnTriggerEnter2D, this will cause only targeted gameobject pass to the Server and it does not pass to the client(this is okay). But previous code TargetedPlayer variable clashed with _TargetedPlayer under void GetTarget functions, it keeps overriding the Network Synchronizing NetworkInstanceId from the Update function. So we must only pass NetworkInstanceId from OnTriggerEnter2D not the Targeted GameObject itself. Later on, then we only get the targeted gameobject under GetTarget function using NetworkInstanceId which got from OnTriggerEnter2D function.
     
    Last edited: May 30, 2016