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
0
Question by M4347 · Jan 27, 2016 at 02:55 PM · raycastmaterialraycastingraycasthit

Need help changing material back when raycast no longer hitting object.

Hi, I have a laser sight on a player which is also using a Raycast to hit an enemy and change its material to a different colour while you have the laser sight pointed at it. The idea being the enemy remains a certain colour while it's being targeted. But I'm not sure how to get the enemy material to change back to its original state when you move the laser sight off of it.

At the moment I'm using this script below to send a Raycast message to change the material in a script on the enemy.

 void Update () {
 
         laserSight.SetPosition (0, transform.position);
         
         shootRay.origin = transform.position;
         shootRay.direction = transform.forward;
 
         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
         {
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             if (enemyHealth != null)
             {
                 enemyHealth.Targeted();
             }
 
             laserSight.SetPosition (1, shootHit.point);
         }
 
         else
         {
             laserSight.SetPosition (1, shootRay.origin + shootRay.direction * range);
         }
     }

And then this is the section of script that's being called on the enemy.

     public void Targeted ()
     {
         Material m = body.material;
         body.material.color = Color.blue;
         print ("targeted");
     }

I'm not really sure how you change it back, as obviously once the laser sight goes off the enemy then the Targeted section is no longer called so I assume I don't do it there. I've tried using a bool in the enemy update function which is set to true when hit whilst starting a coroutine to reset it after a short time, but the problem is this keeps resetting the material whilst still targeting the so the enemy ends up flashing.

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
0

Answer by M4347 · Jan 27, 2016 at 11:09 PM

Ok I've worked out a way around this for any that are interested, though I'm sure there's a more efficient way to do this, so please still tell me if you know it.

At the moment the best way I can find to do this is by creating a small sphere set at the player position with the mesh renderer turned off so it's hidden, and set the collider to a trigger. I've then added a script to the sphere with OnTriggerStay to detect my target with a tag, then change the material in that. Then OnTriggerExit I use to set the material back to the original state. It's then just a case of setting the sphere position to the raycast hit point if the target has my enemyhealth script.

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 LLIV · Jan 28, 2016 at 08:39 PM 1
Share

Ins$$anonymous$$d of making your material change based of a void you should do this: Use a bool as you said however ins$$anonymous$$d of simply setting a timeout make two variables. Put your timeout on one of these variables and write something like if(VariableB == true) {VariableA = true;} use your function to set VariableB to true. However ins$$anonymous$$d of simply saying wait make VariableA false you've created a buffer. The problem you had was your bool would always timeout and reset your material. Then your function would be called again and the process would repeat. If you set VariableB for your true and false you can prevent the change from ever happening (Check the status of VariableB before changing VariableA) this is a little confusing so think off it like this. You set VariableB to true every time you call your function. However you also reset your timer every time you call the function. Something like: int a = 0; void Update () { a++; If (a > 5) { b = false; //changes your material} } void Targeting (){ a = 0; b = true; //changes your material } This way you never actually change the material or set some kind of timeout unless Targeting isn't called for __ frames. Hope that helps despite it being so confusing!

avatar image M4347 LLIV · Jan 29, 2016 at 10:19 PM 0
Share

Thanks for that, I have to admit I had to read the answer a few times before it sunk in! But I completely understand what you mean now! Seems I was along the right lines, just didn't think of having that second variable in the Update function to help with the timeout.

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

46 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

Related Questions

NullReferenceException while raycasting? 0 Answers

An probably very easy question about Raycast system... 1 Answer

Why isn't my ray working? 1 Answer

Can i use raycast for something like a keypad? 2 Answers

Raycast doesn't stay in one place 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