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
3
Question by videoanime · Mar 14, 2015 at 02:49 PM · raycastdistancedrawray

How can I draw a specific distance with Debug.Drawray

I have this issue:

I'm using Physics2D.Raycast as next:

 RaycastHit2D hit_der = Physics2D.Raycast (transform.position+new Vector3(transform.localScale.x/2,0,0),Vector3.right,1,10);
 RaycastHit2D hit_izq = Physics2D.Raycast (transform.position-new Vector3(transform.localScale.x/2,0,0),Vector3.left,1,10);

Where supposedly I have my distance of 1 and it'll be true if I hit my layer 10.

Well, until here there is no problem, but now I want to see the rey with the next:

 Debug.DrawRay(transform.position+new Vector3(transform.localScale.x/2,0,0),Vector3.right);
 Debug.DrawRay(transform.position-new Vector3(0,transform.localScale.y/2,0),Vector3.left);

The problem here is that DrawRay doesn't allow a distance parameter, how can I draw the distance declared in Raycast using Drawray?

I've observed that if I multiply the parameter direction of Drawray, a bigger line is drawn, If I let just the direction without multipliers, is the same as my "1" distance in Raycast?

Thank you.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Glurth · Mar 14, 2015 at 02:53 PM

Rather that use DrawRay, use DrawLine, which allows you to specify both endpoints. http://docs.unity3d.com/ScriptReference/Debug.DrawLine.html

It looks like you already know how to get both the end points you need, from the ray, is that correct?

If not it would be-

end point: (ray.direction.normalized*line_length) + ray.origin

start point: ray.origin

(not sure if you actually need to normalize direction, it SHOULD be normalized already)

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 videoanime · Mar 17, 2015 at 01:54 AM 0
Share

Ohhhhh thank you, I'd not used Drawline, yes, I have my start and end point.

avatar image
0

Answer by abhijit93 · Aug 09, 2017 at 09:50 PM

A simple tweak to the 2nd parameter of the Debug.DrawRay would do.

Incase your Debug.DrawRay was

 Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
 Debug.DrawRay(transform.position, forward, Color.green);

Just change it to

 Debug.DrawRay(transform.position, forward * 1000f, Color.green);
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 Glurth · Aug 09, 2017 at 11:35 PM 0
Share
 Vector3 forward = transform.TransformDirection(Vector3.forward)

Check out: https://docs.unity3d.com/ScriptReference/Transform-forward.html

avatar image
0

Answer by SteenPetersen · Aug 16, 2018 at 08:46 AM

If you are drawing a ray say like this:

 RaycastHit2D hit = Physics2D.Raycast(beginRayCast, dir, myDistance, myMask);


Now you wish to visualize that ray as a debug.drawRay, you can simply do the following:

 Debug.DrawRay(beginRayCast, dir.normalized * MyDistance, Color.blue, 5f);


By normalizing the direction you can now multiply it by your distance to get the same ray.

hope this helps.

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 Saki_col · Nov 08, 2018 at 02:36 PM

Fisrt we need to know what the DrawRay function expects from parameters

It wants the origin point, the direction and the distance of the ray (you can also give it a color, and other parameters).

public static void DrawRay(Vector3 start, Vector3 dir, Color color = Color.white, float duration = 0.0f, bool depthTest = true);

So now we need to know the direction between our ray origin and our ray.point position, to find we can use a substraction...

If one point in space is subtracted from another, then the result is a vector that “points” from one object to the other:

 // Gets a vector that points from the player's position to the target's.
 var heading = hit.point - ray.origin;

Now with this info we can get the direction and the distance of our ray

 var distance = heading.magnitude;
 var direction = heading / distance; // This is now the normalized direction.

And this will be the resulting code...

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 
 if (Physics.Raycast(ray, out hit))
 {
    // Gets a vector that points from the player's position to the target's.
    var heading = hit.point - ray.origin;
    var distance = heading.magnitude;
    var direction = heading / distance; // This is now the normalized direction.
 
    Debug.DrawRay(ray.origin, direction * distance, Color.red);
 
  }

https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

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

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

6 People are following this question.

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

Related Questions

Why doesn't the distance in RaycastAll and DrawRay correspond? 1 Answer

Get the distance between the origin and HitPoint for Raycast? 1 Answer

Is Raycast relatived to screen resolution? 1 Answer

Math - calculate position in world space from ray on infinite plane 2 Answers

Picking up rigidbody objects 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