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 KurtIngamells495 · Jul 01, 2013 at 12:18 PM · raycasttags

Raycast ignore player

I have a script in C# which I have written that allows a player to create and destroy blocks just like in Minecraft. I have NEVER used raycast before and am not very good at it. The problem is that if you look down (I adapted the mouse look script to allow you to look directly up or down - 90 degrees instead of 60), and you right click to destroy the block below you, it destroys the player controller. I'm using the standard 1st person controller. This is the code... all blocks have a "Block" tag, maybe we could use this to our advantage so it only destroys blocks...

 using UnityEngine;
 using System.Collections;
 public class BuildScript : MonoBehaviour
 {    
     //Create a variable for the raycast system to allow it to 'hit' prefabs
     RaycastHit hit;
     
     //Blocks
     public Transform emptyPrefab;
     public Transform grassPrefab;
     public Transform cardboardPrefab;
     public Transform slatePrefab;
     public Transform leatherPrefab;
     public Transform brickPrefab;
     public Transform stonePrefab;
     public Transform dirtPrefab;
     public Transform bedrockPrefab;
     
     //Private variables
     private Transform selectedPrefab;
     private float selectedBlockID;
     
     void Start ()
     {
         //Set the default prefab
         selectedPrefab = grassPrefab;
     }
     
     void Update ()
     {
         //Allow the player to change the selected ID by using the 1-0 keys
         if (Input.GetKey ("1")){selectedBlockID = 1;}
         if (Input.GetKey ("2")){selectedBlockID = 2;}
         if (Input.GetKey ("3")){selectedBlockID = 3;}
         if (Input.GetKey ("4")){selectedBlockID = 4;}
         if (Input.GetKey ("5")){selectedBlockID = 5;}
         if (Input.GetKey ("6")){selectedBlockID = 6;}
         if (Input.GetKey ("7")){selectedBlockID = 7;}
         if (Input.GetKey ("8")){selectedBlockID = 8;}
         if (Input.GetKey ("9")){selectedBlockID = 9;}
         if (Input.GetKey ("0")){selectedBlockID = 10;}
         
         //Make sure that the correct block for the ID is selected    
         if(selectedBlockID == 1){selectedPrefab = grassPrefab;}
         if(selectedBlockID == 2){selectedPrefab = cardboardPrefab;}        
         if(selectedBlockID == 3){selectedPrefab = slatePrefab;}    
         if(selectedBlockID == 4){selectedPrefab = leatherPrefab;}    
         if(selectedBlockID == 5){selectedPrefab = brickPrefab;}
         if(selectedBlockID == 6){selectedPrefab = stonePrefab;}
         if(selectedBlockID == 7){selectedPrefab = dirtPrefab;}
         if(selectedBlockID == 8){selectedPrefab = bedrockPrefab;}
         if(selectedBlockID == 9){selectedPrefab = emptyPrefab;}
         if(selectedBlockID == 10){selectedPrefab = emptyPrefab;}
         
         //Fire the raycast system from the center of the screen in the direction the camera is looking
         Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
         
         //Align newly created blocks to a grid
         Vector3 G = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
         
         //Check what prefab the raycast has hit and allow the player to create/destroy in that position
         if(Physics.Raycast(ray, out hit))
         {            
             //Left click
             if(Input.GetMouseButtonDown(0))
             {
                 //Destroy the block at that position
                 Destroy(hit.collider.gameObject);
             }
             
             //Right click
             if(Input.GetMouseButtonDown(1))
             {
                 //Create a block at that position
                 Instantiate(selectedPrefab, G, Quaternion.identity);
             }
         }
     }
 }
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
0
Best Answer

Answer by KurtIngamells495 · Jul 01, 2013 at 01:54 PM

Problem resolved. Player layer was set to Ignore Raycast, lol

Comment
Add comment · Show 5 · 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 amphoterik · Jul 01, 2013 at 07:33 PM 0
Share

Just be aware that it isn't always viable to put the player on that layer. Sometimes you want certain raycasts to hit the player. Remember that if later you are trying to use raycasts and it isn't working.

avatar image KurtIngamells495 · Jul 02, 2013 at 09:23 AM 0
Share

Yes, thank you for letting me know but I can't think of any time I will be using raycasts that need to hit the player, plus this is really only a test game because I'm new to coding in c# and trying to learn how to generate a world terrain out of blocks and be able to edit it just like in $$anonymous$$C and Starforge, and then to save and load that data in the smallest possible file size. That is the purpose of this project, and in the actual Game once I start making it I doubt I will be using raycast against the player :)

avatar image amphoterik · Jul 02, 2013 at 09:25 AM 0
Share

That's fine. It was mostly an FYI for future consideration. Good luck!

avatar image Fattie · Jul 02, 2013 at 09:25 AM 0
Share

$$anonymous$$urt, you should TIC$$anonymous$$ one of the useful answers. On the OP can close out a question. Also, you get "points" so that next time you can ask questions without moderation. Cheers

avatar image KurtIngamells495 · Jul 02, 2013 at 09:35 AM 0
Share

Thanks, sorry I am so tired it's unbelievable plus this forum isn't the easiest to use, I must say the layout confuses me a lot :L

avatar image
3

Answer by amphoterik · Jul 01, 2013 at 01:34 PM

Set a layermask for your raycast. This will allow you to determine which layers are hit by the raycast and which aren't. This will also solve the issue of the ray stopping at the player when you want it to continue.

More information can be found here: http://answers.unity3d.com/questions/416919/making-raycast-ignore-multiple-layers.html

This answer is provided by Alucardj: It is much easier to declare a variable that is typecast as LayerMask, then in the Inspector, simply tick the layers that you wish to be detected by the raycast. As a test, create a new script, at the top put

 var myLayerMask : LayerMask;

Now save this script, go back to Unity and attach the script to an empty gameObject or the camera (it doesn't matter, this is just a test). Now look in the inspector, click on the drop-down menu then tick the layers you wish to be acknowledged. This is much easier than trying to implement the bitshift method in the documentation. Now you've seen how it works, simply use :

 var myLayerMask : LayerMask;
  
 // in a function
 if ( Physics.Raycast(transform.position, transform.forward, Hit, Range, myLayerMask) )
 {
 // Do Stuff
 }

http://answers.unity3d.com/questions/409360/raycasthit-behind-object.html

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
1

Answer by Cence99 · Jul 01, 2013 at 01:00 PM

  //Left click
  if(Input.GetMouseButtonDown(0) && hit.collider.tag != "Player")
  {
       //Destroy the block at that position
       Destroy(hit.collider.gameObject);
   }

Just check if the ray hasn't hit the Player (you need to tag the Player as "Player" or something else) :)

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 KurtIngamells495 · Jul 01, 2013 at 01:06 PM 0
Share

Thank you so much, that has solved the problem of the player object getting deleted, and it turns out the prefab containing the player controllers didn't have the Player tag applied for some reason, even though normally it does by default. How could I make it so the raycast continues through the player though because now I can't destroy the block underneath me it just does nothing :(

avatar image Fattie · Jul 01, 2013 at 01:35 PM 0
Share

the answer is at hand, Physics Layers ...

http://answers.unity3d.com/questions/382342/npcs-collide-with-collider-but-player-does-not.html

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

I want to setup a raycast that is going forward from my character that allows me to left click on a cube with the tag "npc" and open up a gui window (in C#) 2 Answers

Activating only one of many prefabs with the same tag 1 Answer

How do I change the value of a tag with raycasting? 0 Answers

Enemy health script using raycast not destroying tagged object 1 Answer

Raycast Tag of Hit 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