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 /
avatar image
0
Question by Elie Charest · Jun 06, 2011 at 11:27 PM · raycastlayersraycasthittouchscreen

Raycasthit info only working the first time the object is hit

Greetings, oh wise minds of the Internet.

I've run into an annoying issue while trying to use Physics.Raycast to identify the object hit. I use the following code to check objects on two layers (9 and 10) using the touchscreen (through the Android Remote) and it works, but only the first time I hit the object. Each subsequent hit on that object will do absolutely nothing...I can touch another object on those layers and it will update the Debug.Log message, but then that will only work once, too.

Here is the code:

 var layerMask : LayerMask;
 
 function Start () {
     // Set the layer mask to "Floor" and "Radar"
     layerMask.value = (1 << 9) | (1 << 10);
 }
 
 function Update () {
     for (var touch : Touch in Input.touches) {
         var hit : RaycastHit;
         var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
         Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
         if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask)) {
             Debug.Log(hit.collider.gameObject.name + " ("+hit.collider.tag+") ");
         }
     }
 }

The code seems clean to me...any ideas why this only works the first time I "touch" an object, and not afterwards?

Thanks in advance!

EDIT: after some more testing, it seems the problem is in Debug.Log (I printed the info on-screen using GUI commands and it works...)

This kind of solves my problem, although it's strange that Debug.Log doesn't seem to work in this instance...)

Comment
Add comment · Show 7
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 Chris D · Jun 06, 2011 at 11:33 PM 0
Share

A little off topic, but what does the line layer$$anonymous$$ask.value = (1 << 9) | (1 << 10); mean? I'm having trouble finding documentation on that notation.

avatar image Elie Charest · Jun 06, 2011 at 11:36 PM 0
Share

It's the Bit Shift operator. There is a pretty good explanation here:

http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

avatar image Elie Charest · Jun 06, 2011 at 11:39 PM 0
Share

By the way, I did try without using a layer mask, and the result is the same: the object is identified as hit only the first time, afterwards nothing happens.

avatar image Owen-Reynolds · Jun 07, 2011 at 12:33 AM 1
Share

It's a feature of the Debug screen that it only shows the same message once. Clicking off Collapse will show all.

avatar image Peter G · Jun 07, 2011 at 12:55 AM 1
Share

BTW I believe in the raycast, it should technically be layermask.value. Your code doesn't throw any exceptions though so I'm a little stumped.. I glanced through the script reference and I didn't see any overloaded method that took a layermask or an implicit cast from layermask to int.

I'd be happy to hear what anyone can tell me about this. (I actually didn't know this struct existed until yesterday so I'm happy to learn more about it.)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dreamer · Jun 07, 2011 at 02:08 AM

Uncheck "Collapse" button in your Console.

Comment
Add comment · Show 4 · 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 Joshua · Jun 07, 2011 at 02:09 AM 0
Share

Yep, that´s the reason only the first Debug.Log is showing up.

avatar image Elie Charest · Jun 07, 2011 at 02:51 AM 1
Share

It's always the little things...

Thanks!

avatar image hellcats · Jun 07, 2011 at 02:59 AM 1
Share

@Dreamer, you just learned about that!

http://answers.unity3d.com/questions/126592/my-function-can-not-update.html

avatar image Dreamer · Jun 07, 2011 at 03:05 AM 1
Share

@hellcats Ya, hahahaha~

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# change an object tag wit raycasthit. 2 Answers

Using Layer Filter Raycast 0 Answers

Need raycast to target child, not parent. 0 Answers

Ignore one layermask question 1 Answer

Layer Mask Detection 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