• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MadJohny · Jul 24, 2014 at 01:20 PM · c#shaderspritebarcutoff

Inverting the cutoff property (UI bar)

Hi, I'm making a sprite based hud in my game, and I'm using the cutoff shader to get the bars working, the problem is that the cutoff property is "inverted" in some way, like when it's at 0, it shows everything, and when it's at 1, it shows nothing, and I want the exact opposite (e.g. if I have 100 hp I want it to draw the whole bar, if I have 0 I want it to draw nothing).

Here is my current code for the bar:

     float energyQuanitity = EnergyBar.GetFloat("_Cutoff");
     energyQuanitity = Mathf.Lerp(energyQuanitity, EnergyScript.energy/100f, lerpSpeed * Time.deltaTime);
     EnergyBar.SetFloat("_Cutoff", energyQuanitity);

and here's the full script:

 using UnityEngine;
 using System.Collections;
 
 public class HudScript : MonoBehaviour {
 
     public float lerpSpeed = 10f;
 
     public Color FirstColor = Color.black;
     public Color SecondColor;
     Color currentColor;
     public GUITexture vignette;
 
     public Material EnergyBar;
     public Material TemperatureBar;
 
     void Start () {
         currentColor.a = 1f;
     }
 
     void Update () {
         currentColor = Color.Lerp(FirstColor, SecondColor, GameManager.temperatureChange/30f);
         vignette.color = currentColor;
         
         TemperatureBar.SetFloat("_Cutoff", GameManager.temperatureChange/30f);
 
         float energyQuanitity = EnergyBar.GetFloat("_Cutoff");
         energyQuanitity = Mathf.Lerp(energyQuanitity, EnergyScript.energy/100f, lerpSpeed * Time.deltaTime);
         EnergyBar.SetFloat("_Cutoff", energyQuanitity);
     }
 }

Thanks in advance.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DajBuzi · Jul 24, 2014 at 01:24 PM 1
Share
 float max = 1.0f;
 float inverted = max - value;
avatar image MadJohny · Jul 24, 2014 at 01:26 PM 0
Share

I'm sorry, but could you edit that into the full script? I'll edit the question

avatar image DajBuzi · Jul 24, 2014 at 01:36 PM 1
Share

Its the same problem with the UI to Image positioning. Get the max value. in this case its 1.0f and just subtract the value from your calculations.

 float energyQuanitity = EnergyBar.GetFloat("_Cutoff");
 energyQuanitity = $$anonymous$$athf.Lerp(energyQuanitity, EnergyScript.energy/100f, lerpSpeed * Time.deltaTime);
 EnergyBar.SetFloat("_Cutoff", **1.0f - energyQuanitity**);
avatar image MadJohny · Jul 24, 2014 at 01:45 PM 0
Share

hmmm now it's just acting weird, it doesn't start at max (it kinda defines a new maximum) and it's acting choppy

edit: apparently it's the lerp that is bugging it

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using PBR shader and dynamically switching values 0 Answers

shader for adding thickness to sprites? 0 Answers

Shader Transition over Time possible? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges