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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ThorShaker · Mar 19, 2014 at 01:18 PM · raycastdistanceraycasthit2dlengthdrawray

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

Hi! My problem is that the length/distance of the ray drawn is not the same as the ray being cast, even though the numbers are the same. The ray is only as long as about 2/3 of the ray that is shown. I'm aware that the length of the ray is made differently in DrawRay, but to my knowledge it doesn't matter as long as you use the same value (for me - 7). This problem makes it difficult to test my game. I'm making a 2D sidescroller.

Here's the code. I've only included the relevant part of the code. I don't know, but it might be relevant to mention that the script is attached to a player object with 2 colliders on it.

 var hit : RaycastHit2D[];
 
 function FixedUpdate()
 {
     hit = Physics2D.RaycastAll(transform.position - Vector2(-0.3, 0.2), Vector2(1, 1), 7);
     Debug.DrawRay(transform.position - Vector3(-0.3, 0.2, 0), Vector2(1, 1) * 7, Color.white);
 }

I've tried to replace the dir parameter in DrawRay with a Vector3 but nothing changes. I've also tried checking if the ray is being cast to the side along the z-axis in the scene view, which it doesn't.

Thanks!!

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 trololo · Mar 19, 2014 at 04:57 PM 0
Share

Don't mix Vector2 and Vector3 as you do.

avatar image ThorShaker · Mar 19, 2014 at 10:55 PM 0
Share

Okay. I switched it now.

1 Reply

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

Answer by ThorShaker · Mar 19, 2014 at 04:45 PM

I solved it.

 Debug.DrawRay(transform.position - Vector2(-0.3, 0.2), Vector2(1, 1).normalized * 7, Color.white);

I put in a Vector2 for consistency, as it didn't make a practical difference. The thing that fixes it is ".normalized". For some reason DrawRay doesn't normalize automatically as in RaycastAll, so you have to do it manually. Vector2.normalized from the Script Reference:

Returns this vector with a magnitude of 1 (Read Only). When normalized, a vector keeps the same direction but its length is 1.0.

So when the vector isn't normalized, its length is something other than 1. That explains why I got another length than 7 units.

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

21 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

Related Questions

How can I draw a specific distance with Debug.Drawray 4 Answers

How to control multiple gradients 0 Answers

Raycasting in 2D is not working 0 Answers

OnCollisionEnter2D is not working 0 Answers

How to place an object in the direction of a raycast 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