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 MumboJumbo9 · Jun 23, 2013 at 03:25 PM · raycastmousephysics.raycastscreenpointtoray

Physics.Raycast sometimes does not work?

Hello,

I have some trouble using Physics.Raycast, it does not seem to work properly.

I have something like this:

 var ray:Ray = Camera.main.ScreenPointToRay(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
 var hit:RaycastHit;
 var is_raycast:System.Boolean = Physics.Raycast(Camera.main.transform.position, ray.direction, hit, Mathf.Infinity);

So normally is_raycast should be true if the cursor is on an object. It works most of the time. But sometimes it doesn't work, is_raycast being false.

Does anybody knows what should be the problem?

Thanks for your answers :)

Comment
Add comment · Show 3
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 AlucardJay · Jun 23, 2013 at 03:40 PM 0
Share

Not sure, but first thought is I find it interesting that I've never seen System.Boolean before today, but today I've seen it twice here on UA : http://answers.unity3d.com/questions/479363/why-isnt-this-script-working-beginner-in-coding.html

Where are people reading/discovering System.Boolean?

 var myBool : boolean = false;
 myBool = true;
      
 if ( myBool ) // same as writing -> if ( myBool == true )
 {
     // Do Stuff
 }

second thought : Ray contains an origin and direction, why not just use :

 Physics.Raycast(ray, hit, $$anonymous$$athf.Infinity);

Your script seems unconventional, but hey there's no problem in being unique!

Just curious, do you have the same problem if you use :

 var ray : Ray = Camera.main.ScreenPointToRay( Input.mousePosition );
 var hit : RaycastHit;
 if ( Physics.Raycast(ray, hit, $$anonymous$$athf.Infinity) )
 {
     is_raycast = true;
 }

  • http://docs.unity3d.com/Documentation/ScriptReference/Ray.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

If there is no change, I'll delete this comment, sorry !

avatar image Em3rgency · Jun 23, 2013 at 03:41 PM 0
Share

Is it possible you have other objects in the way that don't have colliders or otherwise block the view to your objects? $$anonymous$$aybe try the Debug.DrawRay function to duoble check.

avatar image MumboJumbo9 · Jun 23, 2013 at 05:10 PM 0
Share

alucardj -> Thanks for the help! However, the result is still is the same. As for "System.Boolean", well I simply didn't know at first about the simple keyword "boolean". I think the first time I used a boolean, $$anonymous$$onodevelop suggested me System.Boolean, but I'm not sure. Anyway, I'm used to write "System.Boolean" ins$$anonymous$$d of "boolean" :).

Em3rgency -> If these objects don't have colliders, how can they block the raycast? $$anonymous$$y first thought was that there must be a collider in front of my object blocking the raycast. But even if it was the case, is_raycast should be true.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Raycast hits everything on the screen 2 Answers

Raycast hit done wrong using ScreenPointToRay coordinates 2 Answers

Detect Mouse in right side or left side For Player? 2 Answers

[2D] Raycast to mouse cursor = offset on Y axis 0 Answers

Prevent raycast to OnMouseOver() or OnMouseEnter() when mouse over UI 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