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 Maenor · Oct 14, 2015 at 11:03 PM · unity 52draycast

How does Raycast2D works? Am i doing it right? [Huge Line]

Hey there!! ^^ I did this little and simple code and in the inspector the Debug.DrawLine, Draws a huge diagonal green line ... Now, the code still returning the data from the if statement ... in other words everything works fine ... but why it does make a diagonal and so large diagonal line? Is that fine? o.O

 void Update(){
 
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
         {
             RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
             if(hit.collider.tag == "//someobject"){
                 Debug.Log("someObjectisHitted");
                 Debug.DrawLine(Input.GetTouch(0).position, hit.point, Color.green, 50);
             }
 
         }

alt text

unityquestion.png (55.2 kB)
Comment
Add comment · Show 2
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 Maenor · Oct 14, 2015 at 11:04 PM 0
Share

The Raycast "Touchedpoint" is the big red cube

avatar image brazmogu · Oct 14, 2015 at 11:49 PM 0
Share

$$anonymous$$aybe you should test the Debug.DrawLine instruction using Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) ins$$anonymous$$d of Input.GetTouch(0).position directly as the origin point.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Oct 15, 2015 at 12:00 AM

What's the line supposed to represent? You're passing the touch position as the first parameter, that's "The position of the touch in pixel coordinates", a Vector2 that will have any value inside your phone's screen resolution.

hit.point, in the other hand is returnig a world position.

Now, Debug.DrawLine expects values in world position, it draws a line in the world from the first parameter to the second one. Your first parameter is being interperted as a world position, probably far away from your camera. So you get a line that comes from some far away point and ends in the hit point.

So, again, what's that line supposed to display? What you send as the first parameter depends on that answer.

Also, Debug.DrawLine and Physics2D.Raycast are not related in any way, the fact that DrawLine shows something weird doesn't mean you should worry about the raycast working or not, you're just drawing the line in the wrong way.

Comment
Add comment · Show 3 · 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 Maenor · Oct 15, 2015 at 01:54 AM 0
Share

I added the line to see how Unity 3D systems vs 2D systems works nothing else a mere tests

avatar image Maenor · Oct 15, 2015 at 01:55 AM 0
Share

And as the question says it works perfectly ... I just want to know Why it shows that line.

avatar image GiyomuGames Maenor · Oct 15, 2015 at 01:59 AM 0
Share

DiegoSLTS answer seems correct as to why the line is like this. $$anonymous$$y guess is that Input.GetTouch(0).position.z and hit.point.z are probably very different so it looks like the line is co$$anonymous$$g from the sky.

So as to "why is shows that line"...it's because your wrote "Debug.DrawLine(Input.GetTouch(0).position, hit.point, Color.green, 50)" in your code :-P

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

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

Related Questions

Raycasting not working? 1 Answer

Linecast, rotate end point relative to objects rotation 1 Answer

Unity Raycast2D results are not as expected? 1 Answer

Animation is not playing all frames 0 Answers

Layer Mask on raycast2d not working? 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