Hello,
I am making a game where you grow crops. I am currently trying to make the soil color when watered darker (gradually). I have tried to search how to gradually make the soil color darker, and it seems that I have to use a gradient. Thing is, I don’t know how to use one, and I can’t really understand the docs. Can someone help me out with this? Thanks.
You call .Evaluate() on the gradient instance, passing in the position you want on the gradient, and it returns the Color at that point.
What part of the example code provided in the docs are you struggling with?
What I don’t understand about the example code in the docs are:
The gradient = new Gradient() part
The colorKey[#].time and colorKey[#].color parts
Gradient.Evaluate[ ]
And the alphaKey[#].alpha and alphaKey[#].time parts
Also, may I have example code about the .Evaluate() part you said? I’m a beginner and I haven’t worked with that yet.
It sounds like you’re struggling with the C# component of it. Are you familiar with C# coding? If so, you should at least understand how gradient = new Gradient() works.
For the timing keys, it’s simpler than it looks. Your gradient is created on a line starting at 0 and ending 1. You can change the color at different values along this line, such as 0.0 or 0.1 or 1.0 for example. And the gradient will blend the colors used at the different positions automatically. The difference between an alpha and a color key is that alpha affects transparency, whereas color is obvious.
So GradientColorKey[ ] creates an array of type GradientColorKeys which stores both Color and Time values (from 0 - 1). The AlphaColorKey[ ] is an array of type AlphaColorKeys which stores both Alpha and Time values. Using these 2 components you can customize your gradient so maybe at position 0.0 you have a semi-transparent red, and at position 0.5 you have a more opaque blue, and at position 1.0 you have an opaque green.
Hopefully that makes sense, but if you’re struggling with the code I would recommend tackling C# as a programming language first before delving into example code in the docs. Best of luck!
All that new stuff is because they are making a Gradient directly in code to prove the point.
You would not normally need to make one like this.
In your (and most typical) use case you make the Gradient field be public and you fill it out in the inspector.
This means you only call .Evaluate() on it to read what the actual color should be.
Yes, I am familiar with C# coding. The part I was confused about was why they put it in there instead of not just making a public gradient and adjusting it in the inspector.
Anyways, thank you both for your help! I have finally gotten the soil to get darker when it gets watered. This problem was bugging me for 2 or 3 days until I finally went on the forums to get a clear answer. I’m going to post the code below if anyone else stumbles on this thread with the same problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoilManager : MonoBehaviour
{
public float soilMoisture = 0;
private SpriteRenderer m_SpriteRenderer;
public Color color;
public Gradient gradient;
private void Start()
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
}
private void Update()
{
color = gradient.Evaluate(soilMoisture);
m_SpriteRenderer.color = color;
}
private void OnParticleCollision(GameObject other)
{
Debug.Log("Particles Hit");
if (soilMoisture < 1)
{
soilMoisture += .01f;
}
else
{
Debug.Log("Watered enough");
soilMoisture = 1;
}
}
}
Might I suggest taking those lines of code out of Update? I’m assuming you have this script on a lot of different objects, so in every single frame you are evaluating the gradient on every single object and then set the color on all of them. But in reality you only need to do that when the moisture value actually changes. Just putting those lines into the block that changes soilMoisture might improve performance by a lot.