Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by ProjectM23 · Jun 22, 2017 at 02:37 PM · unity 5raycastobjectselectionhighlight

How do you highlight an object pointed to by a raycast unity 3d?

Hello everyone, I was wondering how to highlight an object with a raycast in unity 3d. By highlight, I mean make the object pointed to by a raycast turn a chosen color, then when the ray cast isn't touching the object, it goes back to it's normal color.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Vicarian · Jun 22, 2017 at 02:57 PM

 using UnityEngine;
 using System.Collections;

 public class PlayerRayCasting : MonoBehaviour
 {
     public float distanceToSee;
     public string ObjectName;
     private Color highlightColor;
     Material originalMaterial, tempMaterial;
     Renderer rend = null;
 
     void Start()
     {
         highlightColor = Color.green;
     }
 
 
     // Update is called once per frame
     void Update ()
     {
         RaycastHit hitInfo;
         Renderer currRend;
 
         //Draws ray in scene view during playmode; the multiplication in the second parameter controls how long the line will be
         Debug.DrawRay(this.transform.position, this.transform.forward * distanceToSee, Color.magenta);
 
         //A raycast returns a true or false value
         //we  initiate raycast through the Physics class
         //out parameter is saying take collider information of the object we hit, and push it out and 
         //store is in the what I hit variable. The variable is empty by default, but once the raycast hits
         //any collider, it's going to take the information, and store it in whatIHit variable. So then,
         //if I wanted to access something, I could access it through the whatIHit variable. 
 
         if (Physics.Raycast(this.transform.position, this.transform.forward, out hitInfo, distanceToSee))
         {
             currRend = hitInfo.collider.gameObject.GetComponent<Renderer>();
 
             if (currRend == rend)
                 return;
 
             if (currRend && currRend != rend)
             {
                 if (rend)
                 {
                     rend.sharedMaterial = originalMaterial;
                 }
 
             }
 
             if (currRend)
                 rend = currRend;
             else
                 return;
 
             originalMaterial = rend.sharedMaterial;
 
             tempMaterial = new Material(originalMaterial);
             rend.material = tempMaterial;
             rend.material.color = highlightColor;
         }
         else
         {
             if (rend)
             {
                 rend.sharedMaterial = originalMaterial;
                 rend = null;
             }
         }                                                                                    
     }
 }
Comment
Add comment · Show 30 · 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 ProjectM23 · Jun 23, 2017 at 01:06 PM 0
Share

Thanks so much for the response @Vicarian , unfortunately though, this still didn't work. The raycast still hits the object and changes the object's color, but when the raycast is not touching the object anymore, the object still does not go back to it's normal color.

avatar image AtGfx ProjectM23 · Jun 23, 2017 at 01:19 PM 1
Share

Check my answer for that...

avatar image ProjectM23 AtGfx · Jun 23, 2017 at 01:48 PM 0
Share

@AtGfx I tried that method and everything still stays one color.

avatar image Vicarian ProjectM23 · Jun 23, 2017 at 01:36 PM 1
Share

$$anonymous$$ight have to make a new material then. I was hoping to avoid that, but it's not too difficult.

I've updated my original answer.

avatar image Vicarian Vicarian · Jun 23, 2017 at 02:47 PM 1
Share

I've added code to my original answer to handle an edge case, where you pass immediately from one renderer to another, without your raycast going null

Show more comments
Show more comments
avatar image ProjectM23 ProjectM23 · Jun 23, 2017 at 01:45 PM 0
Share

You guys are troopers, thanks for the quick sincere responses. @AtGfx @Vicarian

avatar image Vicarian ProjectM23 · Jun 23, 2017 at 06:59 PM 1
Share

The answer was updated. I didn't have to change anything else in the scene, but you do have another camera in the scene that doesn't need to be there.

Show more comments
avatar image SSL7 · Oct 31, 2017 at 08:30 AM 0
Share

hey! what are you using for renderer and gameobject name?

avatar image
1

Answer by AtGfx · Jun 22, 2017 at 02:44 PM

Hello,

Just set a script that raycast through your scene, then when you hit something you can retrieve the hit object and assign a colour to its material, or you can also set a specific shader that highlight your object, or whatever you want. Then record a reference of your object (tag, name, ID, ...). When you hit an object check if it is a new one (It mean different than the previous one), if it is the case just cancel the selected effect on the previous and enable it on the hit one !

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 ProjectM23 · Jun 23, 2017 at 01:19 PM 1
Share

Thanks @AtGfx for your response, unfortunately, I've already tried this and this method does not work.

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

193 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

Related Questions

State Machine Behaviour OnStateExit doesn't call some time. 0 Answers

I never get inside if (Physics.Raycast(ray, out hit,Mathf.Infinity, touchInputMask) 1 Answer

Raycast on Graphic with Custom Mesh, 1 Answer

How to make objects without the new keyword 0 Answers

Swing on z axis 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