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 Mayday556 · Aug 01, 2013 at 02:01 AM · playerpickupmaterial colorhorrorfunction update

How do I highlight an object when the player is near it

I have been working on a horror game called 'Stalk' for a while now. I am trying to implement a way of picking up items, but there is a problem. I am trying to highlight the object when the player is near it, but because the checking would need to be in the update function, setting the current color/material to a color and then changing it's color wouldn't work because the variable would then be set to yellow, making the object stay yellow. Is there a way to make this work?

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
Best Answer

Answer by pickle chips · Aug 01, 2013 at 02:28 AM

You could just store the original colour in a variable, and then change it back to that once the player is no longer near. For example:

 private Color originalColor;
 
 void Start() {
      originalColor = renderer.material.color;
 }
 
 void Update() {
      if (distance < range) { //Checking if player is near enough
           renderer.material.color = Color.yellow;
      }
      else {
           renderer.material.color = originalColor;
      }
 }
Comment
Add comment · Show 5 · 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 tw1st3d · Aug 01, 2013 at 02:43 AM 0
Share

don't forget to show him how to set the distance, using X or Z coords.

 public Transform playerChar, objToAffect;
 protected int distance = objToAffect.position.x - playerChar.position.x;
avatar image pickle chips · Aug 01, 2013 at 02:48 AM 0
Share

Yes, but it seemed the question was more oriented towards the colour changing, so i left that out :p I guess it would have been a good idea to include it though, thanks for adding it in!

And in case you want the distance on all axes, you can use Vector3.Distance()

avatar image Mayday556 · Aug 01, 2013 at 03:11 AM 0
Share

This actually doesn't work as I am using this to see which object will be picked up and put in the hand, and I am using an array, and assigning all pickable object in an array, and finding which one is the closest.

avatar image nixcs2512 · Aug 01, 2013 at 06:47 AM 0
Share

...I don't know how that answer doesn't work with you? That script can be attached to any objects with mark "pickable"(you can assign manually or by some boolean).By the way, if what you need is finding closest Object in an array and highlight it,so here you are:(this script placed in player gameObject)

  public GameObject[] objarray;//this array is manually assigned
     void Update()
     {
      float closestdistance;
      GameObject closestgameObject;
      for(int i=0;i<objarray.Length;i++)
      {
       float distance = Vector3.Distance(transform.position,objarray[i].transform.position);
       if(distance<=closestdistance)
       {
        closestdistance = distance;
        closestgameObject = objarray[i];
       }
      }
      closetgameObject.renderer.material.color =...//anything you want
     }

Actually above code only show you the "closest" object, it means just one, so i think maybe you don't want something like that. Try this code ins$$anonymous$$d:

 public GameObject[] objarray;
 public float rangefinditem=10f;//items will change color if its distance to player is <= 10 
 void Update()
 {
  for(int i=0;i<objarray.Length;i++)
  {
   float distance =Vector3.Distance(transform.position,objarray[i].transform.position); 
   if(distance<= rangefinditem)
    objarray[i].renderer.material.color =...//anything you want
   else
    objarray[i].renderer.material.color =...//original color
  }
 } 
avatar image Mayday556 · Aug 01, 2013 at 04:13 PM 0
Share

Thank you very much this fixes everything!!!

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

18 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

Related Questions

How to pickup and equip an object? 0 Answers

Upgrades not applying after first death 2 Answers

Pick something up with name - ontrigger 1 Answer

I'm making a horror game and I need help with item pickup 1 Answer

(Help) Pick up item animates to point and becomes a platform 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