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 WilliamKattrup · Dec 09, 2014 at 12:45 AM · raycastcolorminecraft

How to find out if raycast dont hit anything

Hi everyone.i am creating a minecraft like game and atm i am about to create the build system. now i need to find out when the mouse/hit point(middle of screen) over an object then its have to make the block yellow and that work :) but when the mouse/middle leave the block then the block keep being yellow :( how can i make it white again? plz help :) thanks

Regards

Heres my code:

     Ray ray = Camera.main.ViewportPointToRay(new Vector3(.5f, .5f, 0));
     RaycastHit hit;

 
         if(Physics.Raycast(ray, out hit, 10)) {
         if(hit.transform.tag == "BlockOuter") {
             hit.transform.gameObject.renderer.material.color = Color.yellow;
         } else {
             hit.transform.gameObject.renderer.material.color = Color.white;
             
         }
Comment
Add comment · Show 2
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 MajdHamada · Dec 09, 2014 at 01:01 AM 1
Share

You can use .... hit.transform.tag !=...

avatar image Bunny83 · Dec 09, 2014 at 01:28 AM 1
Share

@mh92: No you can not. If the Raycase doesn't hit anything the RaycastHit struct isn't filled with anything. That's why Raycast returns a boolean which tells you if it hits something. Only if it returns true the content of the RaycastHit struct has a meaning.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Linus · Dec 09, 2014 at 01:10 AM

This should work. Also unless you are working on a very small project, you will run into performance issues if you are using colliders for the edges of blocks. Or even "real" blocks at all. You should read up on voxels. I know a great getting started tutorial on voxels, Ill find the link if you want it.

 Ray ray = Camera.main.ViewportPointToRay(new Vector3(.5f, .5f, 0));
 RaycastHit hit;
 Transform lastHitTransform;
      
      
 if(Physics.Raycast(ray, out hit, 10)) {
     //code in here run only if something is hit
         lasHitTransform = hit.transform;
 
          if(hit.transform.tag == "BlockOuter") {
              hit.transform.renderer.material.color = Color.yellow;
          } else {
             //This runs if something other than the tag BlockOuter is hit
              hit.transform.renderer.material.color = Color.white;             
          }
 } else {
     //runs if nothing is hit
     lastHit.renderer.material.color = Color.white;
 }



Edit: Corrected the code, since there is no hit information if you don't hit anything as pointed out by Bunny83

Also here is the link to the voxel tutorial that really like http://netbook-game.blogspot.no/2013/08/unity-voxel-tutorial-part-1-generating.html

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 Bunny83 · Dec 09, 2014 at 01:53 AM 0
Share

@Linus: Well, it makes not much sense to use hit inside the else branch of your Raycast since it won't contain "an object" you "don't hit" ^^

You should save the last hit object in a variable and if you hit a new block or nothing revert the color of the saved object.

avatar image Linus · Dec 09, 2014 at 08:25 AM 0
Share

oh snap that is true. Ill see to update answer. Guess I did not really read that line, for some reason I assumed he changed color of an ai$$anonymous$$g marker.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Click GameObject to change Color 1 Answer

Getting color from mesh renderer causing crash 2 Answers

Pull Color from pixel of a an invisible texture from Raycast. 1 Answer

need Block Placer/Eraser to not destroy any other gameobjects 1 Answer

Unity 3D C# - Gameobject Minecraft Like Placing 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