• 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 alexseikuzmin85 · Feb 13, 2019 at 10:31 AM · raycastraycasthit2d

RaycastHit2D works incomprehensibly

 for(int  = 0; i < allPoints.Count; i++){
 RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, allPoints[i].point);
 if (raycastHit2D.collider != null) 
 {
         Debug.DrawRay(transform.position, raycastHit2D.point, Color.red);
         Debug.Log(transform.position+" "+ allPoints[i].point + "  "+ raycastHit2D.point);
 }
 }

log before change transform.position: (0.0, 0.0, -15.2) (17.3, -10.0) (17.3, -10.0) log after change transform.position: (0.0, 8.0, -15.2) (17.3, -2.0) (17.3, 6.0)

For some strange reason, after changing the position of the object, the direction of the Ray2D shifts

alt text

alt text

before.png (14.2 kB)
after.png (10.5 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
2

Answer by Captain_Pineapple · Feb 13, 2019 at 11:49 AM

Hey there,

well this can't work since you are confusing positions of your points with directions. You have to subtract all points by your transforms position to get a relative direction. Currently your first ray direction is (17.3, -10.0). After moving it is (17.3, -2.0). -> It does exactly what you tell it to do. Draw a ray in this direction.

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
2

Answer by xxmariofer · Feb 13, 2019 at 11:54 AM

hello, i think your issue is understanding Directions as Points. they are both Vector3 but they are not the same. when you are creating the ray, you are not passing points to the ray but directions. for example if you pass a direction to the ray that is

 new Vector3(1,1);

the ray will go in the angle 45* and will have this angle anyway if the camera is at position (0,0,0) or position (0,8,0). so your issue will be fixed when you dont update the allPoints var, i imagine they are points in the editor so just leave them at the exat value, dont update it with the camera position since they are not points, but directions.

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 xxmariofer · Feb 13, 2019 at 11:59 AM 0
Share

this is the exact same response as @Captain_Pineapple so i leave it for clarification and delete it after your problem is fixed.

avatar image
0

Answer by alexseikuzmin85 · Feb 13, 2019 at 12:34 PM

Indeed, I did not quite understand how it works. Now everything fell into place. Thanks!!!

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 xxmariofer · Feb 13, 2019 at 12:45 PM 0
Share

mark captain answer as correct for helpping future readers with the same issue :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Is there a better way to find where to start my raycast? 2 Answers

Player Raycast Does Not Compute 0 Answers

2D click on object with a raycast not working 2 Answers

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

Raycast Randomly Nulls with Movement 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges