Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Needing clarification...code works!!!

Discussion in 'Scripting' started by Quincey, Mar 3, 2020.

  1. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Greetings,

    I'm able to finally post something with asking for help or insight on what I did wrong this time and it feels pretty good. After several hours of trial and error and looking up the script API I figured out how to get my radial fill slider to work the way I needed to for my particular project. However, I'm confused and I wanted someone to clarify if they don't mind as to why my code didn't require Mathf.Lerp. I've posted my code and it works 10,000% I thought Mathf. Lerp meant a float value would interpolate between two other float values? I used a float value for the fill amount going for 0 = no value and 1 = meaning radial fill is full, but somehow mathf.lerp didn't work. Could I get someone to look at my code and explain why this worked perfectly and it's not using a Lerp please? I'm just trying to learn and figure out programming a little more :D


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class EnableUI : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private Canvas canvas;
    10.     [SerializeField]
    11.     private Image button;
    12.     [SerializeField]
    13.     private Image fill;
    14.     [SerializeField]
    15.     private float fillTimer;
    16.     [SerializeField]
    17.     private float startValue;
    18.     [SerializeField]
    19.     private float endValue;
    20.     [SerializeField]
    21.     private float fillSpeed;
    22.  
    23.     void Start()
    24.     {
    25.         //Setting active for testing purposes
    26.         canvas.enabled = false;
    27.  
    28.         fillSpeed = 1f;
    29.         fillTimer = 3f;
    30.         startValue = 0f;
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         ButtonPressed();
    36.     }
    37.  
    38.     private void OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.gameObject.CompareTag("Player"))
    41.         {
    42.             canvas.enabled = true;
    43.         }
    44.     }
    45.  
    46.     private void OnTriggerExit(Collider other)
    47.     {
    48.         if (other.gameObject.CompareTag("Player"))
    49.         {
    50.             canvas.enabled = false;
    51.         }
    52.     }
    53.  
    54.     public void ButtonPressed()
    55.     {
    56.         if (Input.GetKey(KeyCode.E))
    57.         {
    58.             fill.fillAmount += fillSpeed / fillTimer * Time.deltaTime;
    59.         }
    60.         else
    61.         {
    62.            if (Input.GetKeyUp(KeyCode.E))
    63.                 {
    64.                      fill.fillAmount = startValue;
    65.                 }
    66.         }
    67.     }
    68. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Mathf.Lerp() takes three inputs. It uses the third input as a percentage (expressed as 0 to 1), and gives you an linear interpreted (hence "lerp") value between the first input and second input.

    Image.fillAmount also takes an input from 0 to 1 to determine its fraction of fill, if the image is set to fill mode.

    In your case you are specifying your own fillSpeed (1.0) and dividing it by how much time you want it to fill over (3.0), and increasing the .fillAmount by this amount. It's really unrelated to any lerp... when you press E, you are simply adding up at 1/3 per second, so it takes 3 seconds to go from startValue (0.0) to fully filled (1.0)
     
    Quincey likes this.
  3. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46

    Hey Kurt,

    Looks like I'm going to have to pay you for your programming lessons lol, you've really helped me out with a lot of basic questions and I really appreciate the knowledge. I was under the impression that I was still using 3 inputs such as fill value 0, fill value 1, and fill duration, and that's why I thought I needed a Mathf.Lerp to get the desired effect. I assumed (key word assumed lol) that Lerping meant having something gradually increase and that's what I thought my "e" key would do, but it never worked in my code. So I got really lucky and tried making another last min variable called fillspeed and decided to type something that would take the fillspeed / filltimer and throw in Time.deltaTime and lucky for me it worked, but I had no idea how I was going to get my radial slider to work..talk about pure luck.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Indeed! But the difference here is that you actually said in your brain "I don't understand this magic; I better ask someone who might..." That's how you understand MORE magic!

    But yeah, like almost all function calls, Mathf.Lerp is instantaneous: you feed in 3 floats, one comes back. It's up to you to store where you are in the fill amount-wise, and in this case you are storing that in the internal .fillAmount field inside the Image component you call
    fill
    .

    The cool part about software engineering is the magic discovery never goes away. I learn something new every day that I code, and I am not exaggerating.
     
    Quincey likes this.
  5. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Well I'll feel happy when I'm at your expertise and I learn something new everyday. The only thing I'm learning at the moment is patience and how to type frantic lines of code hoping that I'll get lucky lol.