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 /
  • Help Room /
avatar image
0
Question by MontigorJ · Jul 01, 2020 at 08:46 PM · raycastnormalreflectphysics2d.raycast

Raycast2d normal points to the center of the world

Hi, trying to make my 2d raycasts bounce off the surface, but reflection is wrong and i think this is because of my raycast normal points to center and does not stay perpendicular. All of the objects are on the same layer and raycasting works fine, except normal and reflection angle. Normal is the red line in the picture.

ScreenShot: https://imgur.com/KmPWJty

Code:

     [HideInInspector]
         public LineRenderer lineRenderer;
         private RaycastHit2D hit;
         private RaycastHit2D hitOne;
         private RaycastHit2D hitTwo;
         private Vector2 twoDir;
         private Vector2 wallNormal;
         public List<Transform> LaserHit;
         private int LayerMask = 1 << 12;
         public bowScript bs;
     
         void Start()
         {
             lineRenderer = GetComponent<LineRenderer>();
             lineRenderer.enabled = false;
             lineRenderer.useWorldSpace = true;
             
         }
     
         void Update()
         {
             if (bs.sArrowSelected)
             {
                 hit = Physics2D.Raycast(transform.position, transform.right, Mathf.Infinity, LayerMask);
                 LaserHit[0].position = hit.point;
                 lineRenderer.SetPosition(0, transform.position);
                 lineRenderer.SetPosition(1, LaserHit[0].position);
     
                 //find normal
                 wallNormal = hit.normal;
                 //find a reflected vector
                 twoDir = Vector2.Reflect(LaserHit[0].position, wallNormal).normalized;
                 //shoot second raycast towards twoDir direction
                 hitOne = Physics2D.Raycast(LaserHit[0].position, twoDir, Mathf.Infinity, LayerMask);
                 //setting the second position transform as a reflected raycast's position
                 LaserHit[1].position = hitOne.point;
                 //setting the second point of linerenderer go towards reflected position
                 lineRenderer.SetPosition(2, LaserHit[1].position);
                 Debug.DrawLine(transform.position, LaserHit[0].position);
                 Debug.DrawLine(LaserHit[0].position, LaserHit[1].position);
                 Debug.DrawLine(LaserHit[0].position, wallNormal, Color.red);
                 Debug.Log("wallnormal " + wallNormal);
                 
             }
 
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

1 Reply

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

Answer by MontigorJ · Jul 05, 2020 at 01:45 PM

Okay, looks like i solved this problem. First of all it was a stupid idea to debug normal vector with drawline, since it takes vector start and vector end parameters but not the direction. I used drawray to debug. I decied to shoot a ray (Ray2D) from my bow and make Vector reflect work with ray's direction. It works.

           hit = Physics2D.Raycast(bs.ray.origin,bs.ray.direction, 50f,LayerMask);
         //find normal               
         Vector2 wallNormal = hit.normal;
         //find a reflected vector
         twoDir = Vector2.Reflect(bs.ray.direction, wallNormal).normalized;
         //shoot second raycast towards twoDir direction
         hitOne = Physics2D.Raycast(hit.point, twoDir.normalized, 50f, LayerMask);
         //setting the second position transform as a reflected raycast's position
         LaserHit[1].position = hitOne.point;
         //setting the second point of linerenderer go towards reflected position
         lineRenderer.SetPosition(2, LaserHit[1].position);
         Debug.DrawLine(transform.position, LaserHit[0].position, Color.green);
         Debug.DrawLine(LaserHit[0].position, LaserHit[1].position, Color.blue);
         Debug.DrawRay(LaserHit[0].position, wallNormal, Color.red);
         Debug.Log("twodir " + twoDir);`
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

252 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 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

raycast : get normal value of linecast hit 1 Answer

Physics2d.Raycast always returning 0,0 0 Answers

Real concept of Vector2.Reflect 0 Answers

raycasthit.normal sometimes returns zero 1 Answer

Raycast2d does not always work properly 0 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