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 PippyLongbeard · Dec 17, 2013 at 04:21 AM · raycast

Check if RaycastHit variable is different.

Hello, I'm trying to make a script that highlights whatever object I'm looking at using raycasts.It works, except for one problem. When I look from one object to another, both objects stay highlighted instead of one of the objects losing the highlight when I look away. I know exactly why it's happening in the code, but I don't know how to fix it. Code: using UnityEngine; using System.Collections;

 public class HighlightScript : MonoBehaviour {
 
     public RaycastHit hit;
     public Material mat, mat2;
     private bool revert = true;
     bool hasChanged = false;
 
     GameObject go;
 
     // Use this for initialization
     
     // Update is called once per frame
     void Update () {
         Vector3 fwd = transform.TransformDirection (Vector3.forward);
 
         if(Physics.Raycast(transform.position, fwd, out hit, 10)){
             go = hit.transform.gameObject;
             go.renderer.material = mat;
             revert = false;
             hasChanged = true;
 
         }
         else{
             revert = true;
         }
         if(revert)
         {
             if (hasChanged){
                 go.renderer.material = mat2;
             }
         }
 
     }
 }

It might not be the most efficient code, but I just threw it together in a couple of minutes. Basically, I want to check if the object being hit by the raycast has changed. This is what the problem looks like, if it helps: alt text

hghlghtscrn.png (11.2 kB)
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

3 Replies

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

Answer by Visal · Dec 17, 2013 at 05:22 AM

Try this I hope this could help:

 public class HighlightScript : MonoBehaviour {
  
     public Material mat, mat2;
     private Transform lastHittedObject;    
     void Update()
     {
         Vector3 fwd = transform.TransformDirection (Vector3.forward);
         
         if(Physics.Raycast(transform.position, fwd, out hit, 10))
         {
             if(lastHittedObject != null && lastHittedObject != hit.collider.transform)
             {
                 //if the lastHittedObject is not null and different from the current hitted object then
                 //reset the material of the lastHittedObject
                 lastHittedObject.renderer.material = mat2;
             }
             lastHittedObject = hit.collider.transform;
             lastHittedObject.renderer.material = mat;//change the hitted object's material
         }
         else if(lastHittedObject)
         {
             lastHittedObject.renderer.material = mat2;//reset the material of the last hitted object if the raycast fail
             lastHittedObject = null;
         }
     }


Hope this help :)

Comment
Add comment · Show 2 · 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 PippyLongbeard · Dec 18, 2013 at 02:36 AM 0
Share

Thanks for the help. $$anonymous$$y problem was that I was trying to check if a GameObject variable was null, which is impossible. I didn't think to try a Transform variable.

avatar image Visal · Dec 18, 2013 at 02:41 AM 0
Share

Glad I could help. Do your best

avatar image
0

Answer by robertbu · Dec 17, 2013 at 05:20 AM

This is untested, but I might restructure your code like this:

 using UnityEngine;
 using System.Collections;
 
 public class HighlightScript : MonoBehaviour {
     
     public Material mat;
     public Material mat2;
     
     private Transform tr = null;
     
     void Update () {
         RaycastHit hit;
         
         if(Physics.Raycast(transform.position, transform.forward, out hit, 10.0f)){
             Transform trT = hit.transform;
             if (tr != null && tr != trT) {
                 tr.renderer.material = mat2;
             }
             tr = trT;
             tr.renderer.material = mat;
             
         }
         else{
             if (tr != null) {
                 tr.renderer.material = mat2;
                 tr = null;
             }
         }
     }
 }
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
avatar image
0

Answer by 13dnizinski · Dec 17, 2013 at 05:59 AM

 //TRY THIS (I think It will work):
 
 public class HighlightScript : MonoBehaviour {
     public RaycastHit hit;
     public Material mat, mat2;
     bool changed = false;
     GameObject go;
     // Use this for initialization
     // Update is called once per frame
     void Update () {
         if(changed == true) {
             go.renderer.material = mat2;
             changed = false;
         }
         Vector3 fwd = transform.TransformDirection (Vector3.forward);
         if(Physics.Raycast(transform.position, fwd, out hit, 10)){
             go = hit.transform.gameObject;
             go.renderer.material = mat;
             changed = true;
         }
     }
 }
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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer

How to use a raycast in a complete script? C# 1 Answer

Raycasting to turn varible on and OFF 1 Answer

Finding Distance between Angles and Points 2 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