Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 AceLegacy · Apr 25, 2013 at 06:29 AM · floatmathf.lerpmeters

Complicated request for script/help/explanations etc.

Hi, i'm just going to jump right into it.

What i'm trying to do is create a script that will display a little texture that I made, which in other words is a little meter. The idea is a "Panic Meter" it's complicated to explain because it goes more in depth with the game i'm making... But basically I want to have it be displayed on the bottom left of the screen, and every time the player enters a trigger zone with a specific tag, the meter texture will change to the other one(s) i've made. There's 8 meters for 8 bars... And over time the meter will decrease by 1 bar for every 30 seconds (In other words, switching back one texture) ~ when the meter reaches 8 the player basically dies with a message or texture that pops up on the screen.

I know it sounds complicated or easy, i've tried to do this myself but I couldn't do it at all... I don't really know how to use floats as well as some other people. I've tried watching tutorials on how to use floats but they're just way too complicated.

I'm willing to provide more information and reply to any answer/tip. Sorry if the post is too long or boring and loses your attention...

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Apr 25, 2013 at 08:26 AM

Something like this:

 // C#
 using UnityEngine;
 using System.Collections;
 
 public class PanicMeter : MonoBehaviour
 {
     public float decayRate = 1.0f/30.0f; // one level every 30 seconds
     public Texture2D[] textures;
     private static float m_PanicLevel = 0;
     public static float PanicLevel
     {
         get { return m_PanicLevel;}
         set
         {
             // keep value above or equal to 0.0f
             m_PanicLevel = Mathf.Max(value, 0.0f);
         }
     }
     
     void Start()
     {
         useGUILayout = false;
         InvokeRepeating("Decay", 1.0f, 1.0f);
     }
     
     void Decay()
     {
         if (PanicLevel > 0)
             PanicLevel -= decayRate;
     }
     
     void OnGUI()
     {
         if (PanicLevel > 0)
         {
             int index = Mathf.Clamp((int)PanicLevel, 0, textures.Length);
             Texture2D tex = textures[index];
             Rect rect = new Rect(0, Screen.height-tex.height, tex.width, tex.height);
             GUI.DrawTexture(rect, tex);
         }
     }
 }

You can simply use:

 PanicMeter.PanicLevel++;

To increase the panic level by 1. Or set it to a certain level:

 PanicMeter.PanicLevel = 5;

It will be automatically decreased. You just have to add your textures to the textures array (of course in the right order ;) ).

You can attach another script to your trigger zones which will increase the panic level.

Comment
Add comment · Show 4 · Share
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 AceLegacy · Apr 26, 2013 at 12:41 AM 0
Share

so pretty much I can put this script into the First person controller? And then create another script with the Panic$$anonymous$$eter.PanicLevel++; thing and put that into a trigger zone so that it will change the panic meter level?

Also when I try the script in-game, the texture doesn't show up :(

avatar image Bunny83 · Apr 26, 2013 at 11:26 AM 0
Share

This script can be anywhere, either on your player or if you have a more general GUI / HUD object, attach it there.

Have you added your textures to the "textures" array in the inspector? Also if the panic level is 0, no texture will be displayed at all. If you want to display the lowest one all the time, remove the "if (PanicLevel > 0)" from OnGUI

avatar image AceLegacy · Apr 27, 2013 at 01:05 AM 0
Share

cool, I've got the texture working now, thanks :) but i'm still encountering a problem, i've tried to add:

void OnTriggerEnter ( Collider col ) { if (col.tag == "Popup") { Panic$$anonymous$$eter.PanicLevel++; }

 }

to the panic meter script, and the texture doesn't change. And I don't know what to put in a separate script to put in the trigger, do you $$anonymous$$d guiding me through this still?

avatar image Bunny83 · Apr 29, 2013 at 07:18 AM 0
Share

@AceLegacy: The OnTriggerEnter should be in a seperate script which you would attach to your "trigger object". You just have to create an empty GameObject, attach a collider (a BoxCollider for example), set it to "isTrigger" and attach a script which has the above mentioned OnTriggerEnter function.

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Float not working all the time? 1 Answer

Player lives script help 1 Answer

Animation spins wildly after completed 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges