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
2
Question by darthbator · Jan 13, 2013 at 06:06 AM · raycastdebuggizmo

Determine length of Debug.DrawRay ray

So I am running into some issues with seeing an extremely short line when I am drawing a ray using debug.DrawRay. I'm trying to setup a visible rayCast gizmo to get a visualization of the control ray I am using to move my character. Here's the code I am using to do this.

 bool interactionRay (Vector3 location) {
           Ray ray = Camera.main.ScreenPointToRay(location);
            Debug.DrawRay(ray.origin, ray.direction, Color.green);
            return Physics.Raycast(ray, out hit);
     }

I was under the impression that a ray was supposed to cast for an infinite direction until it hits something, however in the editor this is what I am seeing.

alt text

The raycast is obviously working since the character is pathing to it's strike point however the debug ray is not following the same path. It's not even reaching down into a place where it's visible by the game camera. Any idea why this might happen? Have I misunderstood how the Debug ray drawing gizmo works? I don't see any option in the ray object itself to set it's length.

debuglength.png (378.9 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

3 Replies

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

Answer by Eric5h5 · Jan 13, 2013 at 08:48 AM

The direction in DrawRay has a length. In Raycast, the distance is Mathf.Infinity by default. So your DrawRay and Raycast are different; you could use ray.direction*Mathf.Infinity in DrawRay to make them the same.

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 darthbator · Jan 14, 2013 at 05:43 AM 0
Share

$$anonymous$$ultiplying the ray distance by infinity seems to cause the ray to not display at all.

Debug.DrawRay(ray.origin, ray.direction * $$anonymous$$athf.Infinity, Color.green);

I've tried caching the ray in a new var first as well. Same result.

avatar image Nomosoro · Apr 04, 2017 at 11:50 AM 0
Share

It causes the ray to disappear for me as well in Unity 5.5, same as @darthbator addressed. Not sure whether it is a version based issue or a supposed behavior.

avatar image Bunny83 Nomosoro · Apr 04, 2017 at 12:34 PM 4
Share

What Eric ment was that it would be logically the same. However the value infinity in most cases cause problems when used in a coordinate system. For example even the camera has a far clipping plane and would show anything beyond that value. If you want to visualize a ray with infinite length you may only use a "large enough" distance value:

 Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.green);

To add some more background:

The direction is a Vector3. When you multiply a vector with a single value each component is simply multiplied by that value. When using infinity, there are two main problems:

  • multiplying any non-zero value by +infinity it will result in + or - infinity depending on the sign of the value. That would effectively remove any direction information since there is nothing like "half infinity". So a vector like (0.3, -0.2, 0.93) would become (+inf, -inf, +inf).

  • multiplying zero with infinity will result in a NaN (Not a Number) value. That's because mathematically multiplying 0 with infinity is undefined as it could result in any possible value.

Because of those two problems it's impossible to use those values in further calculations.

The "distance" limitation value of the Raycast method is not involved in any direct computation but is simply used in a distance check comparison. So if the resulting distance is greater than that limit, the function simply returns false. If the limit is infinity, no hit distance can be larger so any hit along the ray will count.

avatar image Nomosoro Bunny83 · Apr 04, 2017 at 01:54 PM 0
Share

Although I sure solved the problem by the 10000f way, the insight of infinity manipulation will probably save my future days. From what you said I conclude that the rule is, one use $$anonymous$$athf.Infinity in calculation only when he well knows what the mechanic and math principles behind. Thanks a lot sir Bunny!

avatar image
6

Answer by Taziar · Jan 24, 2014 at 07:49 PM

You can set the distance that the Debug Ray will draw.

Debug.DrawRay(ray.origin, ray.direction * raycastDistance, Color.red, duration);

You simply multiply the Direction by a distance (float). I use the same distance variable I use to cast the actual ray and it works beautifully.

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
avatar image
0

Answer by Chronos-L · Jan 13, 2013 at 06:19 AM

If the yellow line is your correct raycast and the green line is your debug ray, then I'm sure that:

1. You are drawing the Camera.main.ScreenPointToRay()'s ray, not your Physics.Raycast()'s ray. (That's why they are in a different location)

2. By this Debug.DrawRay script ref, in your code, the Debug.DrawRay() will draw for only one frame because the duration parameter is 0 by default.

Comment
Add comment · Show 6 · 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 darthbator · Jan 13, 2013 at 06:34 AM 0
Share

So when I do a Physics.Raycast does it not simply cast the ray that I pass in there? I was thinking of it very much as a bullet->gun analogy. The ray holds the trajectory and origin information and then the Raycast fires it out into the game environment.

The yellow line in the frame is the path the actor is taking from their current location to the destination (which is the hit.point of the Physics.Raycast I am returning from this function).

The issue I am really having is how short the debug ray is. If I increase the duration it does indeed draw longer. Then I just end up with a whole bunch of rays as I move my finger, They are no longer, they just persist. I only want the ray being drawn in frames where the finger is down on the device, so having it draw for a single frame is desired behavior.

avatar image Chronos-L · Jan 13, 2013 at 06:44 AM 0
Share

Ok, I think I miss-phrase my number 1. Yes, you are right for your concept of Physics.Raycast().

But I do not know why there are multiple rays when you move your finger around in game, I don't think that is the default behavior for Debug.DrawRay().

avatar image darthbator · Jan 13, 2013 at 07:11 AM 0
Share

I get multiple rays because the function is getting called a bunch of times as I touch and move my finger around. So if I cast the debug ray for more then 1 frame that's why I see multiples.

avatar image Chronos-L · Jan 13, 2013 at 07:18 AM 0
Share

How about tracking an empty object to your finger, and then draw from that empty object, you should get just 1 single ray.

avatar image darthbator · Jan 13, 2013 at 08:52 AM 0
Share

Yeah I actually started drawing a linecast from the ray origin to the hit location. It's functional I guess but I don't understand why the Debug.Raycast function is behaving as it is. I don't see why that ray would abruptly cut off like that.... A ray extends infinitely unless you decide to truncate it with the raycast correct? The only fields I seem to see in rays are an origin and a direction leading me to believe a ray has no inherent distance.

Show more comments

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

13 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

Related Questions

Raycast feelers not reading in all four directions. 0 Answers

Need help fixing an error. Using raycast to mimic laster beam but no collider, Line Renderer, Raycast Collider. 1 Answer

Debug.draw rect 2 Answers

Draw raycast in final build 2 Answers

Debug.DrawRay not drawing since upgrading my project to 3.3.* 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