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 gloopy · Apr 19, 2013 at 12:13 AM · raycastcolliderlayermaskphysics.raycast

Raycast layer mask problem

Hi all, I am somewhat new to unity but think I have a good understanding of how things work.

My basic problem is that I want to use a layer mask with

 Camera.main.ScreenPointRoRay

I am using a Physics.RayCast from my camera to my player to detect if anything is in the way, if it is then I am hiding it so we can see the player through it using:

 camHit.transform.renderer.enabled = false;


This works fine, but once it's hidden I want to use a layermask so my click to move function works properly (this is what uses the Camera.main.ScreenPointRoRay), otherwise my ray hits the object in the ways collider.

If I disable the object I hit collider my Physics.Raycast hits the player and so hides the object again, then unhides, and loops forever.

I may have gone about doing this in the completely wrong manner, hopefully someone can assist.

Here is my function for detecting if objects are in the way.

 function hideObjects()
 {
 var camHit : RaycastHit;
 var fwd = Camera.main.transform.TransformDirection(Vector3.forward);
 if(Physics.Raycast(Camera.main.transform.position, fwd, camHit, 50))
 {
 print(camHit.transform.tag);
 if(camHit.transform.tag == "roof")
 {    
     theRoof = camHit.transform;
     camHit.transform.renderer.enabled = false;
     camHit.transform.collider.enabled = false;
 }
 else if (camHit.transform.tag == "player")
 {
     theRoof.renderer.enabled = true;
     theRoof.transform.collider.enabled = true;
 }
 }
 Debug.DrawLine (Camera.main.transform.position, camHit.point, Color.yellow, 0.25);
 
 }

and here is a snippet of my click to move function. This is the Ray that will hit the building collider, this is the function I want to put a layer mask on but I don't know how judging by the options on this page:

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html

             if(Input.GetKeyDown(KeyCode.Mouse0))
             {
                 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 //MOVEMENT CODE HERE
             }

Thanks in advance :)

Mike

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
1
Best Answer

Answer by AlucardJay · Apr 19, 2013 at 04:16 PM

Check my answers here :

  • http://answers.unity3d.com/questions/416919/making-raycast-ignore-multiple-layers.html

  • http://answers.unity3d.com/questions/279514/raycast-ignore-trigger-colliders.html

Comment
Add comment · Show 3 · 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 gloopy · Apr 21, 2013 at 03:05 PM 0
Share

Thank you I will have a look at those.

avatar image gloopy · Apr 24, 2013 at 05:22 PM 0
Share

Yes!

It's now working. I was trying to do my layer mask in the wrong place. Your posts showed me the way to go!

Thank you very much. I will name a character in my game after you...

avatar image AlucardJay · Apr 24, 2013 at 05:24 PM 0
Share

Awesome =]

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

12 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

Related Questions

A question about "Physics.Raycast()" 1 Answer

if ( Physics.Raycast ( ray, out hit, Mathf.Infinity, LayerMask ) ) not working? 2 Answers

Why are raycasts so unreliable? 2 Answers

RaycastHit returns object without collider, in wrong layer 1 Answer

LayerMask for RayCast 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