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 /
This question was closed Oct 21, 2015 at 04:07 PM by Azumiar for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Azumiar · Oct 19, 2015 at 06:15 PM · c#raycasttransformlayermask

Raycasting on mask doesnt work

Hello!I have a little problem.I'm using raycasting for my player to move(Click to move),but there are some elements on screen wich down want to be hitted by a raycast(Example:I click on UI elements and my player moves towards that click position wich i dont want).I tried using layerMasks for this but it's not working for me somehow.I want my raycast hit only background image so i set it to layer BG wich is 8 but when i use the following code the ray casts on every mask but I need it to cast only on 8th layer. In my code I set layermask to 1<<8 but still not working. Code:

   void Update () {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Input.GetMouseButtonDown(0)) {
          if(Physics.Raycast(ray, out hit, 1000,1 << 8)){
             target = new Vector2(hit.point.x,hit.point.y);
         }
     }
     transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 }


Thank you for your attention!

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by equus_ligneus · Oct 21, 2015 at 01:53 PM

First of all, there is nothing wrong with your raycast code and it should hit everything on the layer with index 8 (which is the 9th layer). However, GUI works slightly different.

The new Unity UI System (UGUI) does not block raycasts in the sense of being a physical object with a collider (TL;DR: you can't just raycast against a Overlay-Type GUI). But there is a way to check whether the mouse cursor is over a UI-element: Asking the EventSystem. In every scene with a Canvas there's an EventSystem. It has a public method IsPointerOverGameObject() that returns true whenever there is a UI-Element under your mouse cursor. So what you do is:

 EventSystem eventSystem = FindObjectOfType<EventSystem>();
 
 // there is no event system (no gui) or no gui element under the cursor
 if(eventSystem == null || !eventSystem.IsPointerOverGameObject())
 {
     // do your raycast
 }

To avoid confusion: EventSystem is in namespace UnityEngine.EventSystems so you need to include this namespace in the beginning of your file.

I further recommend saving the EventSystem reference as a variable of your class, FindObjectOfType () is a slow operation.

Comment
Add comment · Show 1 · 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 Azumiar · Oct 21, 2015 at 04:06 PM 0
Share

Thanks!Works perfectly!!!

avatar image
1

Answer by TH_Unity · Oct 20, 2015 at 06:55 PM

The code seems right to me. Try to use LayerMask.LayerToName(string name) to prevent using a wrong layer index.

I guess the main problem is Camera.main. The GUI camera might differ from the camera you are trying to cast the ray on. If you do not have a world GUI the camera would most likely be far away from the GUI. Notice that the Ray you get returned from Camera.ScreenPointToRay(Input.mousePosition) is in world coordinates and depends on the camera's position.

I think you have to use the EventSystem of Unity to catch a click on a GUI element.

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

Follow this Question

Answers Answers and Comments

33 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

Related Questions

How do I move a raycast back on objects z axis? 1 Answer

Enemy gameObject didn't move 1 Answer

Drawing flat arrows 1 Answer

Place Gameobject within a specific tranform value 3d 3 Answers

Help with casting a raycast C# 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