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 Marcus79 · Oct 11, 2021 at 07:32 AM · gameobjectraycasthighlight

How do I highlight the color of a material through raycasting?

Hi guys,

I've been pondering over this for way too long. Basically what I'm trying to achieve is to highlight an object when the crosshair detects an object. This is a 3D game, with a crosshair in the middle. If the player walks up to the object within range, the crosshair turn red indicating something can be done with the object. This works perfectly, but now I'd like to actually highlight the object by getting the original material attached to the gameobject and just change its color.

All the tutorials I've come across let you assign a totally different material on raycast, not something I want to do, just get the original color on the material, change it once the raycast detects it and restore the original material (without the color change) when the ray no longer detects the object.

This is what I have so far that works:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Rendering;
  
 public class Highlights : MonoBehaviour {
  
     public Material highlightMaterial;
     Material originalMaterial;
     GameObject lastHighlightedObject;
  
 
     void Update()
         {
                HighlightObjectInCenterOfCam();
         }
 
     void HighlightObject(GameObject gameObject)
     {
         if (lastHighlightedObject != gameObject)
         {
             ClearHighlighted();
             originalMaterial = gameObject.GetComponent<MeshRenderer>().sharedMaterial;
             gameObject.GetComponent<MeshRenderer>().sharedMaterial = highlightMaterial;
             lastHighlightedObject = gameObject;
         }
  
     }
  
     void ClearHighlighted()
     {
         if (lastHighlightedObject != null)
         {
             lastHighlightedObject.GetComponent<MeshRenderer>().sharedMaterial = originalMaterial;
             lastHighlightedObject = null;
         }
     }
  
     void HighlightObjectInCenterOfCam()
     {
         float rayDistance = 1000.0f;
         // Ray from the center of the viewport.
         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
         RaycastHit rayHit;
         // Check if we hit something.
         if (Physics.Raycast(ray, out rayHit, rayDistance))
         {
             // Get the object that was hit.
             GameObject hitObject = rayHit.collider.gameObject;
             HighlightObject(hitObject);
         } else
         {
             ClearHighlighted();
         }
     }
  
     
 }

As you can see, it assigs a totally different material once it hovers over the detected gameobject, this just needs to be replaced by the orginal material + a color change. But I have no clue how to do that? I assume the public Material highlightMaterial needs to be removed, and something needs to happen inside the HighlightObject function.

Any help would be greatly appreciated!

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 xxmariofer · Oct 11, 2021 at 07:50 AM

change

      public Material highlightMaterial;
      Material originalMaterial;

for

      public Color highlightColor;
      Color originalColor;

and

          originalColor = gameObject.GetComponent<MeshRenderer>().sharedMaterial.color;
          gameObject.GetComponent<MeshRenderer>().sharedMaterial.color = highlightColor;
          lastHighlightedObject.GetComponent<MeshRenderer>().sharedMaterial.color = originalColor;
Comment
Add comment · Show 1 · 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 Marcus79 · Oct 11, 2021 at 08:00 AM 0
Share

Yes!! That worked! I was overcomplicating things! Thank you very much for you time to respond!

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

233 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

why gameobject / raycast when dragged is not accurately following the touch position in unity 0 Answers

Unity 2021 LTS: IPointerEnterHandler is broken 1 Answer

why is part of my script being ignored? 2 Answers

GameObject facing hit.point doesn't always work 1 Answer

How to Pick up Gameobject 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