Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 BradStanden · Aug 05, 2019 at 11:57 AM · lerpsliderhudnpc

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;





Comment
Add comment · Show 1
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 DCordoba · Aug 05, 2019 at 12:32 PM 1
Share

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);
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by BradStanden · Aug 05, 2019 at 01:29 PM

@DCordoba Thank you so much, that worked a treat.

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

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

112 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 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 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 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 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

Unity Health Slider Lerp in Exact seconds. 1 Answer

Add "drag" to slider 0 Answers

Script updates prefab but not instantiated clone of prefab 0 Answers

Gradient issue with slider 1 Answer

Mathf.lerp not finishing the lerp(age) 1 Answer


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