- Home /
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...
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.
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 :(
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
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?
@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
Follow this Question
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