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

Need help! Taking over someones project, need help to understand and where to go from here.

Discussion in 'Scripting' started by ALGRIFF, Nov 2, 2017.

  1. ALGRIFF

    ALGRIFF

    Joined:
    Nov 16, 2016
    Posts:
    11
    Okay so i am new to posting here, but i am currently working on a VR project where in the scene the leaves on the trees need to shrink and disappear and the water level needs to drop as well. the idea is to just play out a scene in VR whilst the person watches. Currently nothing happens. I've attached the main script let me know if you need anything else thanks.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class DecayBehavior : MonoBehaviour {
    6.  
    7.     [Header("Config")]
    8.     public float Decay;
    9.  
    10.     [Header("Hud")]
    11.     public GameObject WaterLevelIndicatorObj;
    12.  
    13.     [Header("Geo Groups:")]
    14.     public GameObject Grp_TreesAObj;
    15.     public GameObject Grp_TreesBObj;
    16.     public GameObject Grp_TreesCObj;
    17.     public GameObject Grp_GrassAObj;
    18.     public GameObject Grp_GrassBObj;
    19.     public GameObject Grp_GrassCObj;
    20.     public GameObject Grp_BushesAObj;
    21.     public GameObject Grp_BushesBObj;
    22.     public GameObject Grp_BushesCObj;
    23.     public GameObject Grp_GroundObj;
    24.  
    25.     [Header("Water:")]
    26.     public GameObject Grp_WaterObj;
    27.     Vector3 WaterPosition;
    28.     public float Water_Low;
    29.     public float Water_High;
    30.  
    31.     [Header("Driven keys properties:")]
    32.     public Animator DrivenKeysAnimator;
    33.     public GameObject WaterDriverObj;
    34.     public GameObject TreesADriverObj;
    35.     public GameObject TreesAColorDriverObj;
    36.     public GameObject TreesBDriverObj;
    37.     public GameObject TreesBColorDriverObj;
    38.     public GameObject TreesCDriverObj;
    39.     public GameObject TreesCColorDriverObj;
    40.     public GameObject GrassADriverObj;
    41.     public GameObject GrassAColorDriverObj;
    42.     public GameObject GrassBDriverObj;
    43.     public GameObject GrassBColorDriverObj;
    44.     public GameObject GrassCDriverObj;
    45.     public GameObject GrassCColorDriverObj;
    46.     public GameObject BushesADriverObj;
    47.     public GameObject BushesAColorDriverObj;
    48.     public GameObject BushesBDriverObj;
    49.     public GameObject BushesBColorDriverObj;
    50.     public GameObject BushesCDriverObj;
    51.     public GameObject BushesCColorDriverObj;
    52.     public GameObject GroundCrossFadeDriverObj;
    53.  
    54.     [Header("Fx properties:")]
    55.     public GameObject DustObj;
    56.     bool DustFlag = false;
    57.  
    58.     Renderer rend;
    59.     Color Col;
    60.  
    61.    // int Frame = 0;
    62.    // bool HalfFpsFlag = false;
    63.  
    64.     ParticleSystem DustSystem;
    65.     ParticleSystem.EmissionModule DustModule;
    66.  
    67.     // ----------------------------------
    68.     void Start() {
    69.         DustSystem = DustObj.GetComponent<ParticleSystem>();
    70.         DustModule = DustSystem.emission;
    71.  
    72.         rend = Grp_GroundObj.GetComponent<Renderer>();
    73.         rend.material.SetFloat("_CrossFade", 0);
    74.  
    75.         WaterLevelIndicatorObj.transform.localPosition = new Vector3(0, (-Decay + 0.5f)*5f, 0);
    76.     }
    77.  
    78.     // ----------------------------------
    79.     void Update() {
    80.  
    81.  
    82.         UpdateDrivenKeysAnimator();
    83.  
    84.        /*
    85.         if (HalfFpsFlag)
    86.         {
    87.             Frame++;
    88.             Application.CaptureScreenshot("Screenshot"+Frame.ToString()+".jpg");
    89.         }
    90.         HalfFpsFlag = !HalfFpsFlag;
    91.         */
    92.  
    93.     }
    94.  
    95.     // ----------------------------------
    96.     void LateUpdate()
    97.     {
    98.     }
    99.  
    100.     // ----------------------------------
    101.     void UpdateDrivenKeysAnimator()
    102.     {
    103.         if (Decay < 0) Decay = 0;
    104.         if (Decay >= 1f) Decay = 1f;
    105.  
    106.         DrivenKeysAnimator.Play("DrivenKeys", 0, Decay);
    107.     }
    108.  
    109.     // ----------------------------------
    110.     void UpdateWaterLevel()
    111.     {
    112.         WaterPosition = Grp_WaterObj.transform.localPosition;
    113.         WaterPosition.y = Water_Low + (WaterDriverObj.transform.localPosition.y * ( Mathf.Abs(Water_Low-Water_High)  ) );
    114.         Grp_WaterObj.transform.localPosition = WaterPosition;
    115.  
    116.         rend = Grp_GroundObj.GetComponent<Renderer>();
    117.         rend.material.SetFloat("_CrossFade", GroundCrossFadeDriverObj.transform.localPosition.y);
    118.     }
    119.  
    120.     // ----------------------------------
    121.     void UpdateTrees()
    122.     {
    123.         UpdateTreeMaterials(Grp_TreesAObj.transform, 1 - TreesADriverObj.transform.localPosition.y, TreesAColorDriverObj.transform.localPosition.y);
    124.         UpdateTreeMaterials(Grp_TreesBObj.transform, 1 - TreesBDriverObj.transform.localPosition.y, TreesBColorDriverObj.transform.localPosition.y);
    125.         UpdateTreeMaterials(Grp_TreesCObj.transform, 1 - TreesCDriverObj.transform.localPosition.y, TreesCColorDriverObj.transform.localPosition.y);
    126.     }
    127.  
    128.     // ----------------------------------
    129.     void UpdateGrasses()
    130.     {
    131.         UpdateGrassMaterials(Grp_GrassAObj.transform, 1 - GrassADriverObj.transform.localPosition.y, GrassAColorDriverObj.transform.localPosition.y);
    132.         UpdateGrassMaterials(Grp_GrassBObj.transform, 1 - GrassBDriverObj.transform.localPosition.y, GrassBColorDriverObj.transform.localPosition.y);
    133.         UpdateGrassMaterials(Grp_GrassCObj.transform, 1 - GrassCDriverObj.transform.localPosition.y, GrassCColorDriverObj.transform.localPosition.y);
    134.     }
    135.  
    136.     // ----------------------------------
    137.     void UpdateBushes()
    138.     {
    139.         UpdateTreeMaterials(Grp_BushesAObj.transform, 1 - BushesADriverObj.transform.localPosition.y, BushesAColorDriverObj.transform.localPosition.y);
    140.         UpdateTreeMaterials(Grp_BushesBObj.transform, 1 - BushesBDriverObj.transform.localPosition.y, BushesBColorDriverObj.transform.localPosition.y);
    141.         UpdateTreeMaterials(Grp_BushesCObj.transform, 1 - BushesCDriverObj.transform.localPosition.y, BushesCColorDriverObj.transform.localPosition.y);
    142.     }
    143.  
    144.     // ----------------------------------
    145.     void UpdateTreeMaterials(Transform Parent, float AlphaValue, float MainColor)
    146.     {
    147.         foreach (Transform U in Parent)
    148.         {
    149.             foreach (Transform T in U)
    150.             {
    151.                 rend = T.GetComponent<Renderer>();
    152.                 if (rend != null)
    153.                 {
    154.                     string[] splitString = rend.material.name.Split(new string[] { "_" }, StringSplitOptions.None);
    155.                         for (int i = 0; i < rend.materials.Length; i++)
    156.                         {
    157.                             if (splitString[0] == "Leaves" || splitString[0] != "FacingLeaves")
    158.                             {
    159.                                 Color c = rend.materials[i].color;
    160.                                 Col.r = MainColor;
    161.                                 Col.g = MainColor;
    162.                                 Col.b = MainColor;
    163.                                 Col.a = AlphaValue;
    164.                                 rend.sharedMaterials[i].color = Col;
    165.                             }
    166.                             else{
    167.                                 Col.r = MainColor;
    168.                                 Col.g = MainColor;
    169.                                 Col.b = MainColor;
    170.                                 rend.sharedMaterials[i].color = Col;
    171.                             }
    172.                     }
    173.                 }
    174.             }
    175.         }
    176.     }
    177.  
    178.     // ----------------------------------
    179.     void UpdateGrassMaterials(Transform Parent, float AlphaValue, float MainColor)
    180.     {
    181.         foreach (Transform U in Parent)
    182.         {
    183.             foreach (Transform T in U)
    184.             {
    185.                 rend = T.GetComponent<Renderer>();
    186.                 if (rend != null)
    187.                 {
    188.                         for (int i = 0; i < rend.materials.Length; i++)
    189.                         {
    190.                         Color c = rend.materials[i].color;
    191.                         Col.r = MainColor;
    192.                         Col.g = MainColor;
    193.                         Col.b = MainColor;
    194.                         Col.a = AlphaValue;
    195.                         rend.sharedMaterials[i].color = Col;
    196.                         }
    197.                 }
    198.             }
    199.         }
    200.     }
    201.  
    202.     // ----------------------------------
    203.     public void SetDecay(float Value)
    204.     {
    205.         Decay = Value;
    206.  
    207.         if (Decay < 0) Decay = 0;
    208.         if (Decay >= 1f) Decay = 0.99f;
    209.  
    210.         if (Decay > 0.1f && DustFlag == false)
    211.         {
    212.             DustFlag = true;
    213.             DustModule.rateOverTime = 10;
    214.         }
    215.         if (Decay < 0.9f && DustFlag == true)
    216.         {
    217.             DustFlag = false;
    218.             DustModule.rateOverTime = 0;
    219.         }
    220.  
    221.         RefreshAssets();
    222.  
    223.         Resources.UnloadUnusedAssets();
    224.     }
    225.  
    226.     // ----------------------------------
    227.     void RefreshAssets()
    228.     {
    229.         WaterLevelIndicatorObj.transform.localPosition = new Vector3(0, (-Decay + 0.5f) * 5f, 0);
    230.  
    231.         UpdateWaterLevel();
    232.         UpdateTrees();
    233.         UpdateGrasses();
    234.         UpdateBushes();
    235.     }
    236.  
    237.     // ----------------------------------
    238.     public float GetDecay()
    239.     {
    240.         return Decay;
    241.     }
    242.  
    243.  
    244. }
    245.  
     

    Attached Files:

    Last edited: Nov 3, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Firstly, use code tags on the forum for proper formatting. Second, you haven't actually asked a question.
     
  3. ALGRIFF

    ALGRIFF

    Joined:
    Nov 16, 2016
    Posts:
    11
    Okay, the main issue im having is i need to change the color of the leaves and i need to make them shrink (to make the trees look like they're dying) this code is "supposed" to do that but does not. the way i was thinking of doing it was to increase the alpha cutoff on the texture and also change the color there as well. but i am unsure on how to do this. I suppose my question would be what is the best way to go about doing that? Sorry this was my first post so i don't know how to use it properly yet.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Well first thing first, it doesn't do anything because nothing calls those UpdateXXX methods.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    KelsoMRK likes this.
  6. ALGRIFF

    ALGRIFF

    Joined:
    Nov 16, 2016
    Posts:
    11