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 /
This question was closed Jul 31, 2014 at 03:58 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Leroterr · Jul 31, 2014 at 01:55 PM · raycasttouchraycastinglayermaskphysics.raycast

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

Why isn't it working when I put in that condition?

It works fine when I remove it, but I really need to make it so that it will only be executed when the player is touching the specific LayerMask.

EDIT: **not touching the specific LayerMask.

When I remove it, the code inside gets executed properly but it still gets executed even when I'm not touching the object with the said LayerMask.

I don't want it to get executed when it's not touching the LayerMask, and when I put in the condition to make it so, it doesn't get executed at all, wherever I touch the screen.

Here's the code I'm using...

 using UnityEngine;
 using System.Collections;
 
 public class TouchMovement : MonoBehaviour {
 
 private RaycastHit hit;
 
 public static bool moving;
 
 Ray ray;
 
 int currTouch;
 int touch2watch;
 
 LayerMask layerZ;
 
 void Start ()
     {
         layerZ = 1 << 12;
             //All layers except the 12th one...
         layerZ = ~layerZ;
 
         touch2watch = 64;
 
         moving = false;
     }
 
 void Update ()
     {
 if(Input.touchCount > 0)
         {
             for(int i = 0; i < Input.touchCount; i++)
             {
 
                     currTouch = i;
 
                     //If the player hits any position on the screen EXCEPT for the object with the layerZ...
                     if(Physics.Raycast(ray, out hit, Mathf.Infinity, layerZ))
                     {
                         //Write "touched"
                         Debug.Log ("touched");
 
                     //Why is Debug.Log ("touched") not being executed? Including the ones below it...
                     //When I remove the condition "if(Physics.Raycast...", it works fine.
                     //But I need it so that this block of code will only be executed when the player is not hitting the object with the LayerZ.
 
                         if(Input.GetTouch(i).phase == TouchPhase.Began && !moving)
                         {
                             touch2watch = currTouch;
 
                             if(touch2watch == currTouch)
                             {
                                 moving = true;
                             }
 
 
                         }
 
                     }
 
 
                 if(touch2watch == currTouch)
                 {
 
                     if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                         touch2watch = 64;
                     }
 
                     if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                         moving = false;
                     }
 
                 }
 
 
                 
             }
 
             
         }
     }
 }

Thanks!

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

Answer by meat5000 · Jul 31, 2014 at 02:32 PM

There's no information in your Ray

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 Leroterr · Jul 31, 2014 at 03:05 PM 0
Share

Thanks! I knew I was missing something simple.

avatar image meat5000 ♦ · Jul 31, 2014 at 03:56 PM 0
Share

You're Welcome

avatar image
1

Answer by dbrizov · Jul 31, 2014 at 02:13 PM

Right now you are ignoring the 12th layer. If you want to hit only the 12th layer, you need to make sure only the 12th bit of the layer mask is ON. Right now the bits from 0 to 11 are ON and the 12th bit is OFF.

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 Leroterr · Jul 31, 2014 at 02:17 PM 1
Share

Yeah that's what I wanted to happen, which is why I inversed it. I want to be able to hit anywhere on the screen except the 12th layer. But with my current code, it doesn't work anywhere on the screen. It completely ignores everything, and I'm not sure why.

Sorry, I forgot to write above that that was I was going for.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Raycast does not work with touch input but works well with mouse input why? 0 Answers

RaycastHit returns object without collider, in wrong layer 1 Answer

Raycast hit in OnDrawGizmos but not in Update 1 Answer

Raycasting UI elements - Without using Event Triggers 0 Answers

2d raycasting to touch position not working 2 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