1

I've been trying to make a rocket league type game with unity 5.4 and in c#.

I've got everything with the players working and the playarea. The players are spawned by the network spawner and a ball is spawned by a ball spawner, like the enemyspawner in the tutorials. I have two goals that the ball can collide with and that resets the ball position. But how do I tell the players that there has been a goal? I've been looking everywhere and can't find a good exampel of how to pass information to the players from the ball. I have a text UI that shows on all players camera and that uses 2 variablers that count score, so what I want is to somehow increase those variables via the ball when it collides with goal.

RPC calls only work between player objects as I understand it, and syncvars does not seem to work if I try to have them between the ball and the players, anyone have any tips or can tell me what I'm missing?

EDIT: For people with the same problem. I solved this by making a scoreManager object and letting it have the scores as syncvars, then I just got the scoremanager component and its script and increased the score at the ball, then get it for the players aswell and get the score.

Morti
  • 175
  • 1
  • 7

2 Answers2

1

Assuming you're using UNet. This is from tutorial with health synchronization:

   [SyncVar(hook = "OnChangeHealth")]  
   public int currentHealth = maxHealth;

   void OnChangeHealth(int cH)
   {
        healthBar.value = (float)cH / maxHealth;
   }

Where healthBar is slider.

Here you have explanation about [ClientRPC] and [Command] http://docs.unity3d.com/Manual/UNetActions.html

Krajca
  • 367
  • 1
  • 8
  • I tried this but it never gave any changes on the players, I will have to look at it again I guess, but as I understood it, it worked since the enemy and the players used the same health script, in this case the ball and the players do not share the same script – Morti Aug 17 '16 at 12:41
  • In my test game (based on this tutorial) only players have health scripts. Even then i think your client app have the same UI and script so it's same. Do you inherit your script from `NetworkBehaviour`? – Krajca Aug 17 '16 at 13:37
  • Yes I do, I also tried giving the ball localclientauthority – Morti Aug 17 '16 at 14:01
  • Then try this: use `[Command]` to increase score on the server and at end of this function send `[ClientRpc]` to send that new value to clients. Or try debug your game maybe goal doesn't trigger at all – Krajca Aug 17 '16 at 14:24
  • I dont understand, I've given the ball prefab client local authority, then I made 2 syncvars (greenscore, redscore). In the balls script, then I have the same sync variables in the playercontroller script. But these are never updated. The ball does collide with the goals but the rpc function that counts the score is not updating the variables in the playercontroller script, what am I missing! – Morti Aug 20 '16 at 15:58
1

For people with the same problem. I solved this by making a scoreManager object and letting it have the scores as syncvars, then I just got the scoremanager component and its script and increased the score at the ball, then get it for the players aswell and get the score.

Morti
  • 175
  • 1
  • 7