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 DoctorMoney · Nov 02, 2013 at 04:02 AM · raycastdebugpickuplayermask

Problems with raycast

I'm using a script that another person helped me with to detect if the player is looking at an object that he can pick up and then picking it up. I'm using raycasts for this and it's always been inaccurate as hell and i couldn't find out why. So i debugged it with:

                       if (Input.GetKeyDown(KeyCode.F))
                     { 
                         Debug.DrawLine (ray.origin, hit.point, Color.red,2);
                     }

and found that the line goes to 0,0,0 in the world instead of forward from where the camera is facing, picture shows the lines

alt text

You can see I'm looking up but the lines still go down regardless of where i look.

The code i'm using is

 public var crosshair : GUITexture;
 var playerFlashlight : GameObject;
 var player : GameObject;
 var layermask : int = 8;
 var cameraA : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     var ray : Ray = cameraA.camera.ViewportPointToRay(crosshair.transform.position);
 
     var hit : RaycastHit;
                       if (Input.GetKeyDown(KeyCode.F))
                     { 
                         Debug.DrawLine (ray.origin, hit.point, Color.red,2);
                     }
     
     if(Physics.Raycast(ray, hit, layermask))
        {
 
               if (hit.transform.name == "flashlight PICKUP" && Vector3.Distance(transform.position, player.transform.position) <= 3)
               {
 
               print ("YOU'RE HITTING IT");
                   if (Input.GetKeyDown(KeyCode.E))
                     { 
                       playerFlashlight.GetComponent("LightScript").acquired = true;
                       playerFlashlight.GetComponent("LightScript").on = true;
                       Destroy (hit.transform.gameObject);
                     }
 
             }
 
     }  
 
 }

I messed with layermasks to see if it was hitting the capsule but i don't think that's the problem based on the results of the line test.

If anyone has any ideas why this is happening it'd be helpful. Thanks

red lines.jpg (312.5 kB)
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
0
Best Answer

Answer by robertbu · Nov 02, 2013 at 04:20 AM

You are calling your Debug.DrawLine() before you do the Physics.Raycast(). At that point in the code, 'hit' is uninitialized. In particular (since a RaycastHit is a struct), hit.point will be Vector3.zero. Insert lines 15 - 18 just inside the Raycast() at line 22. Then you will get a true reading of the what is going on:

BTW, I don't see any problems with the Raycast() code. You might want to put the following line at line 22 so that you can see what the Raycast() is hitting. Often with Raycast() problems, the issue is the raycast either hitting things that the programmer did not expect or colliders that are not aligned correctly. The line:

 Debug.Log(hit.transform.name+", "+hit.transform.tag);
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 DoctorMoney · Nov 02, 2013 at 04:29 AM 0
Share

i see, so my original assumption was right that the ray cast is hitting the players collider because it works fine and is hitting the player when i look down which explains why i have to be at an awkward angle to pick up the flashlight. I don't really understand layer masks, you think you could edit my code so the layermask would work?

the layer the collider is on is 8 which is titled "Player" out of 10 layers that i have. i set the layermask int to 8 but that doesn't seem to be doing anything.

And thank you for helping me with this, i spent all day on this one problem and was having a really hard time with it @robertbu

avatar image robertbu · Nov 02, 2013 at 04:39 AM 0
Share

Layer masks are bit sets. You don't refer to a layer in a layermask by the layer number. The easiest way to fix your code would be to change line 4 to:

 var layermask : Layer$$anonymous$$ask;

Then you can go to the inspector and select the specific layer by name. If you really want to assign numbers, then layer 8 will be a value 256 if you are counting from 0. Or you can use:

 layermask = 1 << 8; 
avatar image DoctorMoney · Nov 03, 2013 at 02:51 AM 0
Share

@robertbu sorry i was at work and then something was happening at home and couldn't get back to you. I tried using the first line of code and it says

BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit, UnityEngine.Layer$$anonymous$$ask)' was found.

and the second line doesn't seem to work as everytime i look down at what i'm picking up it tells me i'm looking at the player and not the object

avatar image robertbu · Nov 03, 2013 at 02:58 AM 0
Share

Actually this may account for some of your problems. The error is right that there is no Raycast with that signature. The raycast was treating your 'layermask' as a distance in your original code. Use this form of Raycast() ins$$anonymous$$d:

 static function Raycast(ray: Ray, hitInfo: RaycastHit, distance: float, layer$$anonymous$$ask: int): bool;

So your line of code would be:

 if(Physics.Raycast(ray, hit, $$anonymous$$athf.Infinity, layermask))
avatar image DoctorMoney · Nov 03, 2013 at 03:05 AM 0
Share

@robertbu tried it at first but it didn't work and then i realized i set it to only raycast the player layer which caused it to only look at the player.

But it works now (i think) and doesn't pick up the player at all.

Thank you so much you've helped me alot

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

15 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

Related Questions

Using unity 4.6 UI and raycast2D functionality 0 Answers

Help Can't find the Correct Parameters 1 Answer

Layer Mask Detection 2 Answers

Physics2D, weird layerMask behavior on Linecast, Raycast and OverlapPoint 2 Answers

raycast ignore layer by default not using layermask. 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