Need suggestions for implementing a mechanic based on the brightness of materials.

Hi anyone reading this. Hope you’re having a good day. I am very much a beginner, who has only recently passed the phase of having to follow a step by step tutorial. I run into issues every five minutes, but still. Basically, I am trying to work on a small top-down parkour game where the player jumps between colourful towers. Naturally, the idea of top-down parkour is flawed because of depth perception meaning you have little way of telling how far from the ground you are. Knowing this, I would like to implement a system which darkens these towers the higher above the towers the player is. Meaning that as the player falls, the tower should become brighter, until they land on it, at which point it is just a normal, fully bright object. I thought that the best way to implement this would be a class, which, it doesn’t take a genius to figure out is terrible code (PS. This wasn’t complete):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaterialChange : MonoBehaviour
{

    public Material tower1Material;
    public Material tower2Material;
    public Material tower3Material;
    public Material tower4Material;
    public Material tower5Material;
    public Material tower6Material;
    public Material tower7Material;
    public Material tower8Material;
    public Material tower9Material;
    public Material tower10Material;

    public Color tower1Color;
    public Color tower2Color;
    public Color tower3Color;
    public Color tower4Color;
    public Color tower5Color;
    public Color tower6Color;
    public Color tower7Color;
    public Color tower8Color;
    public Color tower9Color;
    public Color tower10Color;


    public class EnvironmentHeight
    {
        public float tower1;
        public float tower2;
        public float tower3;
        public float tower4;
        public float tower5;
        public float tower6;
        public float tower7;
        public float tower8;
        public float tower9;
        public float tower10;

        public EnvironmentHeight(float firstTower, float secondTower,
                                    float thirdTower, float fourthTower,
                                    float fifthTower, float sixthTower,
                                    float seventhTower, float eighthTower,
                                    float ninthTower, float tenthTower)
        {
            tower1 = firstTower;
            tower2 = secondTower;
            tower3 = thirdTower;
            tower4 = fourthTower;
            tower5 = fifthTower;
            tower6 = sixthTower;
            tower7 = seventhTower;
            tower8 = eighthTower;
            tower9 = ninthTower;
            tower10 = tenthTower;
        }
    }

    void Start()
    {
        EnvironmentHeight level1EnvironmentHeights = new EnvironmentHeight(0f, 0f, 0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);

       
       
    }
   
    // Update is called once per frame
    void Update()
    {
        BrightnessChange();
    }

    void BrightnessChange()
    {
        PlayerMovement playerMovementCall = new PlayerMovement();
        float verticalPlayerPosition = playerMovementCall.publicCharacterYPosition;

        tower1Color.r = verticalPlayerPosition / 10;
    }
}

As you can probably guess, I have doubts about this code. In case you’re wondering, I was going to instantiate the class for every new level with 10 variables, each with the height of the towers. Also, each tower was going to be a different colour, which is why I made 10 material reference variables.

Is there any way to make this system work without cluttering code with ten of each variable ( ten was going to be the maximum amount of towers that a level would have, which would be another thing that would be good to not have the restriction of.) I hope I gave enough detail. Thanks for reading.

Hey!

First of all, when you have lots of variables (tower1Material, tower2Material, …) use an Array or a List to manage them. This will make your life just so much easier. Secondly, visual-only effects such as the one you describe are best handled in shaders. There you would be able to ‘easily’ affect visuals based on their distance, which you can get from the z-buffer. However, as shaders are a bit closer to hardware, i cant say i’d recommend them to a beginner.

There are some new’ish tools Unity provides tho, to allow beginners to write shaders more easily. So maybe take a look at Shader Graph. It’s a node based system, so no manual writing of shaders requird. The z-buffer i mentioned above would be the Scene Depth Node… i believe. I cant really say much more, since i didnt find enough time to play around with Shader Graph yet, but it’s definitely worth checking out.

Also, i would highly recommend checking out the gamedev series on youtube by Sebastian Lague. It’s pretty well made, and seeing as you didnt know about arrays and lists yet, most certainly worth checking out. He also offers little exercises, so you can test your skills.

Hope this helps :slight_smile:

1 Like

First off, thanks for replying. I did actually know about arrays from the beginner scripting tutorials by Unity, but I forgot about them. Also, I’ll try to get to the tutorials you mentioned at some point, but right now I’m trying to just get some kind of game done because I want to learn to finish games. I’ll be looking into shader graphs and seeing if I can find the solution to the problem.