Search Unity

Changing RenderSettings.ambientSkyColor only locally? (is it possible?)

Discussion in 'Multiplayer' started by christopf, Jun 22, 2017.

  1. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Hi,

    maybe it's a super easy (or super stupid) question but i'm already going insane in my brain T_T

    How do i approach to change the ambientSkyColor (for different light cycles) only localy and not on server side.
    In the end i want for every player a different light setting depending on the triggers. But right now the settings are changing globally so everyone has the same settings (with higher frequency of changing).

    kind regards
    chris
     
  2. martaaay

    martaaay

    Joined:
    Apr 13, 2009
    Posts:
    136
    Unless I miss my guess, this setting is not special (e.g. Doesn't auto replicate to all clients). Can you show your code for how you're setting it?
     
  3. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    I will try since my code is quite a mess :>

    So i have 2 Main objects, the main mech/the world itself and one for the trigger points.
    Both have a lot of childs but in case of the main mesh it shuld be irrelevant.

    The world has this script (i try to simplify it that it might be more readable and hope to not cut the error along):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class WakeUp: NetworkBehaviour {
    6.  
    7.     public Color inside = new Color(0.05490196f, 0.02352941f, 0.03137255f, 1f);
    8.     public Color outside = new Color(0.9411765f, 0.7529412f, 0.7529412f, 1f);
    9.     private Color bday = new Color(0.02352941f, 0.03529412f, 0.5294118f, 1f);
    10.  
    11.     [HideInInspector] public bool nighttime = false;
    12.     [HideInInspector] public bool intransition = false;
    13.  
    14.     void Awake() {
    15.  
    16.         RenderSettings.ambientLight = outside;
    17.     }
    18.  
    19.     void Update () {
    20.  
    21.         if (intransit) {
    22.             RenderSettings.ambientLight = TheNewerCycleOfLifeAndDeath(nighttime);
    23.         }
    24.     }
    25.  
    26.     private Color TheNewerCycleOfLifeAndDeath(bool daytime) {
    27.  
    28.         Color lightingNow = RenderSettings.ambientLight;
    29.         if (daytime) {
    30.             if (lightningNow == outside) {
    31.  
    32.                 intransition = false;
    33.                 nighttime = false;
    34.                 return lightningNow;
    35.             }
    36.             return Color.Lerp(lightningNow, outside, Time.fixedDeltaTime * .5f);
    37.         }
    38.         else {
    39.             if (lightningNow == inside) {
    40.  
    41.                 intransition = false;
    42.                 nighttime = true;
    43.                 return lightningNow;
    44.             }
    45.             return Color.Lerp(lightningNow, inside, Time.fixedDeltaTime * .5f);
    46.         }
    47.     }
    48. }
    49.  
    the trigger parent is this:
    triggerAmbientlight1.PNG

    the tuerwaechterin (german for doorkeeper) script looks like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Tuerwaechterin : NetworkBehaviour {
    6.  
    7.     public enum doorposition {
    8.  
    9.         inner,
    10.         outer,
    11.         somethingelse,
    12.     }
    13.  
    14.     private WakeUp zzz;
    15.     [HideInInspector] public bool goingInside = false;
    16.     [HideInInspector] public bool goingOutside = false;
    17.     [HideInInspector] public doorPos _doorPos;
    18.  
    19.     void Awake() {
    20.  
    21.         zzz = FindObjectOfType<WakeUp>();
    22.         _doorPos = doorposition.somethingelse;
    23.     }
    24.  
    25.     public void light (bool night) {
    26.  
    27.         zzz.intransition = true;
    28.         zzz.nighttime = night;
    29.     }
    30. }
    and all childs of that trigger parent are those invisible plane meshes acting as triggers:
    triggerAmbientlight2.PNG

    tuerwaechterkinder (doorkeeper childs) script is this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Tuerwaechterkinder : NetworkBehaviour {
    6.  
    7.     [SerializeField] private Tuerwaechterin.doorposition state;
    8.  
    9.     private Tuerwaechterin mama;
    10.  
    11.     void Awake() {
    12.  
    13.         mama = GetComponentInParent<Tuerwaechterin>();
    14.     }
    15.  
    16.     void OnTriggerEnter (Collider other) {
    17.  
    18.         if (other.tag != "Player") { return; }
    19.  
    20.         if (state == Tuerwaechterin.doorposition.outer) { mama.goingInside = true; }
    21.         if (state == Tuerwaechterin.doorposition.inner) { mama.goingOutside = true; }
    22.  
    23.          mama._doorPos = (mama._doorPos == Tuerwaechterin.doorposition.somethingelse) ? state : mama._doorPos;
    24.  
    25.         if (mama.goingInside && mama.goingOutside) {
    26.             bool nighttime = (mama._doorPos == Tuerwaechterin.doorposition.outer) ? false : true;
    27.             mama.light(nighttime);
    28.         }
    29.     }
    30.  
    31.     void OnTriggerStay (Collider other) {
    32.  
    33.         if (other.tag != "Player") { return; }
    34.     }
    35.  
    36.     void OnTriggerExit (Collider other) {
    37.  
    38.         if (other.tag != "Player") { return; }
    39.         if (state == Tuerwaechterin.doorposition.outer) { mama.goingInside = false; }
    40.         if (state == Tuerwaechterin.doorposition.inner) { mama.goingOutside = false; }
    41.  
    42.         if (!mama.goingInside && !mama.goingOutside) {
    43.  
    44.             mama._doorPos = Tuerwaechterin.doorposition.somethingelse;
    45.         }
    46.     }
    47. }
    ////////////

    All in all i use confusing var titles i think and the code might be a "little" messy, i dont know, i'm auto didact. But i think the problem is somewhere else not necessarily in the trigger logic (which is working fine if you play solo).
    Maybe it doenst need extend NetworkBehaviour at all?

    Thanks for everyone who takes the time to get into this <3
     
    Last edited: Jul 3, 2017
  4. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Does someone else might try his/her take on this when i change the variables names for easier read?
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Have you tried using isLocalPlayer?
     
  6. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Yes, tried it on OnTriggerEnter() @Tuerwaechterkinder and on Update() @Aufwach (world script).
    For the first nothing happened as far as i remember and right now i saw, i shouldh ave tested it on OnTriggerExit as well, forgot this one.
    For the second it disabled the whole mechanic for the actual player but miraculous made something else - not connected at all - working more fluently :eek:
     
  7. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Tested to add isLocalPlayer = return for OnTriggerStay() and OnTriggerExit() as well, not only OnTriggerEnter() - did not work.

    Tried changing all 3 scripts from NetworkBehaviour to MonoBehaviour - did not work as well.

    Any other ideas?
     
  8. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Some1 else got a spin on this?
    I'm out of ideas and can't find anything in the web :(
     
  9. Deleted User

    Deleted User

    Guest

    Have you tried enabling the networkbehaviours only for their corrosponding client? (Btw try naming your variables in english so others can understand your code better)

    -Speedex
     
  10. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    How to i enable the networkbehaviours that way? Sounds like exactly what i'm searching for :3

    And i changed the variable names, i hope i didnt mess it up on its way through though :3
     
  11. Deleted User

    Deleted User

    Guest

    Add a new networkbehaviour script (this one needs to be active tho!)to your player prefab, disables all other networkbehaviours in the inspector. Then in your newly made script add the following:

    public override void OnStartLocalPlayer()
    {
    //Enable all scripts you disabled in the inspector
    }

    Hope that works out for you!

    -Speedex
     
  12. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Seems not to work.

    Code (CSharp):
    1.     public override void OnStartLocalPlayer() {
    2.  
    3.         base.OnStartLocalPlayer();
    4.  
    5.         // start the tuerwaechterin & tuerwaechterkinder scripte
    6.         Tuerwaechterkinder[] knut = FindObjectsOfType<Tuerwaechterkinder>();
    7.         for(var i = 0; i < knut.Length; i++) {
    8.  
    9.             knut[i].enabled = true;
    10.         }
    11.  
    12.         Tuerwaechterin knutmama = FindObjectOfType<Tuerwaechterin>();
    13.         knutmama.enabled = true;
    14.  
    15.         WakeUp lore = FindObjectOfType<WakeUp>();
    16.         lore.enabled = true;
    17.     }
    Well it does work for the changing and that the objects get enabled. But it doesnt in terms of only on client side.
     
  13. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Feels like i'm getting closer, lovely irc chat helped me a lot this morning.

    SO:

    - if i change RenderSettings.ambientLight manually in the editor, it works! (=it only changes locally)
    - if RenderSettings.ambientLight gets changed via the the WakeUp script only (changing by clicking in inspector), it works!

    So the problem seems to be at the last 2 scripts, Tuerwaechterkinder and Tuerwaechterin.
    I thought it might be the FindObjectOfType<WakeUp> method but when i replace it with dropping the relevant GameObject into an empty field via editor before starting, it doenst work as well.
     
  14. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Who needs an obfuscator when you can use German names for variables?
     
    TwoTen likes this.
  15. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    Is there an option to check if an gameobject (like the on activating the OnTriggerEnter method) is connected to a certain NetworkConnectionId?
     
  16. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    If you mean NetworkInstanceId. Then just grab the NetworkIdentity component and get it's netId
     
  17. christopf

    christopf

    Joined:
    Dec 28, 2015
    Posts:
    17
    as it seems i got it working with a close variant to my first attempts.
    i checked for the NetworkIdentitiy component in the colliding gameobject and took it isLocalPlayer boolean T_T

    i tried this before and it didnt work:
    Code (CSharp):
    1.  
    2.     void OnTriggerEnter (Collider other) {
    3.         if (!isLocalPlayer) { return; }
    4.         if (other.tag != "Player") { return; }
    5.         // relevant code...
    6.     }
    this whatsoever works though!:
    Code (CSharp):
    1.  
    2.     void OnTriggerEnter (Collider other) {
    3.         if (other.tag != "Player" || !other.GetComponent<NetworkIdentity>().isLocalPlayer) { return; }
    4.         // relevant code...
    5.     }