- Home /
Creating a HUD that indicates how close the player is to an NPC
Hi,
I am very new to unity and learning from online sources. I am creating a maze based game as part of a PhD on human threat processing.
I am trying to create a HUD that shows the user how close an NPC is to the players position. I want to use a slider that fills up and changes from green to red as the NPC gets closer to the player. I have managed to create a basic HUD that works in stages (code below) but want this to be a smoother and not staged. I have tried to use Lerp but have struggled to make it work. The below script is attached to a rigidbodyFPS. Can anybody offer any advice?
Cheers,
Brad
public class PlayerScript : MonoBehaviour
{
private GameObject triggeringNPC;
private bool triggering;
public GameObject npcText;
public Slider DDSlider;
public Transform NPC;
float DDDistance;
int CircaDist = 1;
int PostDist = 2;
int PreDist = 4;
void Start()
{
DDSlider.value = 0;
}
void Update()
{
if (Vector3.Distance(transform.position, NPC.position) <= CircaDist)
{ DDSlider.value = 100;
GameObject fill = DDSlider.transform.GetChild(1).GetChild(0).gameObject;
Image fillImage = fill.GetComponent<Image>();
Color newColour = new Color(1f, 0f, 0f);
fillImage.color = newColour;
}
else if (Vector3.Distance(transform.position, NPC.position) <= PostDist)
{ DDSlider.value = 50;
GameObject fill = DDSlider.transform.GetChild(1).GetChild(0).gameObject;
Image fillImage = fill.GetComponent<Image>();
Color newColour = new Color(1f, 1f, 0f);
fillImage.color = newColour;
}
else if (Vector3.Distance(transform.position, NPC.position) <= PreDist)
{ DDSlider.value = 25;
GameObject fill = DDSlider.transform.GetChild(1).GetChild(0).gameObject;
Image fillImage = fill.GetComponent<Image>();
Color newColour = new Color(0f, 1f, 0f);
fillImage.color = newColour;
}
else
{ DDSlider.value = 0;
GameObject fill = DDSlider.transform.GetChild(1).GetChild(0).gameObject;
Image fillImage = fill.GetComponent<Image>();
Color newColour = new Color(0f, 0f, 0f);
fillImage.color = newColour;
some tips:
you dont need to call getcomponent/navigate the hierachy each time, just put that on start
create global variables to keep some memory directions between frames
some classes like colors (or Vector3) have some predefined values
currDistance is the curent distance, dimDistance is the inverse percent between 0 and 4
the following code is not tested, but I believe is wht you need, or at least you can take some ideas~
public class PlayerScript : $$anonymous$$onoBehaviour
{
private GameObject triggeringNPC;
private bool triggering;
public GameObject npcText;
public Slider DDSlider;
public Transform NPC;
float DDDistance;
int CircaDist = 1;
int PostDist = 2;
int PreDist = 4;
GameObject fill;
Image fillImage;
float CurrDistance;
float DimDistance;
void Start()
{
DDSlider.value = 0;
fill = DDSlider.transform.GetChild(1).GetChild(0).gameObject;
fillImage = fill.GetComponent<Image>();
}
void Update()
{
CurrDistance = Vector3.Distance(transform.position, NPC.position);
DimDistance = $$anonymous$$athf.Clamp ((4f-CurrDistance)/4f, 0f, 1f) ;
DDSlider.value = 100f*DimDistance;
fillImage.color = Color.Lerp(Color.green, Color.red, DimDistance);
}
}
Your answer
Follow this Question
Related Questions
Gradient issue with slider 1 Answer
Mathf.lerp not finishing the lerp(age) 1 Answer
Change the Shape Unity's UI Sliders 1 Answer
Continuous number scroll picker UI using image and Lerp? 0 Answers
How to lerp health slider 1 Answer