- Home /
Crafting System - How to use a slider to show progress
Hi there,
I'm currently developing the crafting system for my 2D Sidescrolling RPG and I'd like to have a Slider appear and fill up over 3-5 seconds whilst in contact with the node and holding down a specific button, after which the node disappears and the ore appears in their inventory. I'd also like it to stop and disappear if the player stops holding the button down or stops being in contact with the node.
My current script is as follows (it works and does the above minus the slider and timing part):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CraftingNode : MonoBehaviour
{
public GUISkin skin;
public Transform gatherBarPrefab;
public Transform player;
public Slider gatheringSlider;
private string tooltip;
private string gatherText;
private bool hovering;
private string nodePos;
BoxCollider2D nodeCollider;
Inventory inv;
PlayerController pc = new PlayerController();
void Start ()
{
nodeCollider = GetComponent<BoxCollider2D>();
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
inv = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
}
void Update ()
{
if(pc == null)
{
pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
}
/*if(gatheringSlider == null)
{
gatheringSlider = GameObject.Find("GatheringSlider").GetComponent<Slider>();
}*/
}
void OnGUI()
{
Event e = Event.current;
GUI.skin = skin;
tooltip = nodeCollider.gameObject.name;
gatherText = "Hold 'Loot' button to Gather " + nodeCollider.gameObject.name + ".";
if(hovering == true)
{
GUI.Box(new Rect(e.mousePosition.x + 10, e.mousePosition.y, 100, 50), tooltip, skin.GetStyle("Tooltip"));
}
if(pc.touchingNode == true)
{
GUI.Box(new Rect(nodeCollider.gameObject.transform.position.x + 10, nodeCollider.gameObject.transform.position.y + 10, 200, 65), gatherText, skin.GetStyle("Tooltip"));
if(Input.GetButtonDown("Loot"))
{
inv.AddItem(13000);
}
}
}
void OnMouseOver()
{
hovering = true;
}
void OnMouseExit()
{
hovering = false;
}
}
I'm also having trouble with this specific line, because it shows it in from the top left corner, rather than at the location of the node:
GUI.Box(new Rect(nodeCollider.gameObject.transform.position.x + 10, nodeCollider.gameObject.transform.position.y + 10, 200, 65), gatherText, skin.GetStyle("Tooltip"));
Your answer
Follow this Question
Related Questions
How to make slider load variable on start 1 Answer
field of view slider starting point 1 Answer
Display actual number of bullets 1 Answer
Set Slider value but don't trigger On Value Changed 4 Answers
Gui script help 0 Answers