Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by segana · Jan 09, 2014 at 03:33 PM · c#physicsraycast

Physics2D.Raycast Reflect issue

I know I'm just being an idiot, but I'm pulling my hair out so thought I'd turn to the experts for help.

Quick background, currently working on a script that will project a trajectory however I can't get the raycasting to work properly.

I can cast a ray, have it hit a collider and then cast another ray in the reflected direction, however I can't then reflect another ray.

The below isn't my actual code, but I created a greatly simplified version to work out the issue. If I do the following:

 void Update () 
     {
 
 
         ray = new Ray2D (goTransform.position, goTransform.up);
 
         Debug.DrawRay (ray.origin, ray.direction * 50, Color.cyan);
 
         hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, 1 << LayerMask.NameToLayer("Middleground"));
 
         inDirection = Vector3.Reflect(ray.direction,hit.normal);
 
         Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
         Debug.DrawRay (hit.point, inDirection * 100, Color.red);
 
         ray = new Ray2D(hit.point, inDirection);
 
         prevHit = hit.point;
 
         hit = Physics2D.Raycast(hit.point, inDirection, Mathf.Infinity, 1 << LayerMask.NameToLayer("Middleground"));
 
         inDirection = Vector3.Reflect(inDirection, hit.normal);
 
         Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
         Debug.DrawRay (hit.point,inDirection * 100, Color.green);
 
     }

The issue is that for the second Ray, it doesn't register a hit.point so every Ray after the first one just uses the same co-ordinates.

Again I'm clearly being an idiot and doing something monumentally stupid, but would really appreciate someone pointing out to me what stupid thing I'm doing/not doing.

Thanks for any help anyone can provide :)

Matt

Comment
Add comment · Show 1
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 semiessessi · Jan 09, 2014 at 05:00 PM 0
Share

it would be nice if you can post the actual code or at least something that would work in principle... its hard to tell what could be wrong when the first line of code in the function won't even compile (!)

1 Reply

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

Answer by robertbu · Jan 09, 2014 at 04:53 PM

I simplified and played a bit with your code. In testing, I found that raycasting from the hit.point sometimes returned the collider that was just hit. Moving the casting point a bit up the normal seems to solve this problem. Here is the code I used that was working correctly for me:

 void Update () 
 {
     Debug.DrawRay (transform.position, transform.right * 50, Color.gray);
     RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, Mathf.Infinity);
     if (hit != null && hit.collider != null) {
         Vector3 inDirection = Vector3.Reflect(transform.right,hit.normal);
     
         Debug.DrawRay (hit.point, inDirection * 100, Color.red);
         hit = Physics2D.Raycast(hit.point+hit.normal*0.01f, inDirection, Mathf.Infinity);

         if (hit != null && hit.collider != null) {
             inDirection = Vector3.Reflect(inDirection, hit.normal);
             Debug.DrawRay (hit.point,inDirection * 100, Color.green);
         }
     }
 }

Note that my original cast was from transform.right rather than transform.up.

Comment
Add comment · Show 6 · 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 segana · Jan 09, 2014 at 08:52 PM 0
Share

You sir are a legend!!

I applied your fix (+hit.normal*0.01f) to my original code and it worked flawlessly first time.

Thank you, thank you, thank you.

On a side note, my original script of some time back used the old Physics.Raycast calls, but as I'm working on a 2D game I thought I'd switch over to the newer code introduced in 4.3.

The original raycast worked perfectly, but the exact same code switched to Physics2D.Raycast (and the associated calls) has the issue I described above. Just wondering if you think this is a bug with the newer 2D features?

avatar image robertbu · Jan 09, 2014 at 09:49 PM 0
Share

Bug or feature, only your application knows for sure. Apparently in 2D, a Raycast from anywhere inside a collider produces a hit. For some things it is a very nice behavior. For example I've seen some clean code using this fact to detect mouse hits on 2D objects. When I saw the behavior of your code I suspected that due to floating point imprecision, the code might detected hit.point as being inside.

avatar image Sanctus2099 · Jul 17, 2014 at 10:55 AM 0
Share

I believe this is not a bug within Unity. If the origin of your ray is inside a collier then it will hit it. In your case the origin is exactly on the collider but due to floating point precision errors it probably ends up being inside. You can safely use collision layers (Layer$$anonymous$$ask) for this.

avatar image Retrovius · Jul 27, 2018 at 01:48 AM 0
Share

Alternatively, you can maintain the direction and origin of the ray from the first reflection point by adding the (direction*0.01f), rather than moving it up by the normal vector. For example:

 Vector2 nextDirection = Vector2.Reflect(firestRay.direction, hit.normal);
 Vector2 nextStartPoint = hit.point + nextDirection * .01f;
 Ray2D nextRay = new Ray2D(nextStartPoint, nextDirection);
avatar image ArIaNFury008 · Aug 27, 2020 at 11:52 AM 0
Share

thank you this so usefull but how can use this in game(Not Scence) ? LineRendere ?

Show more comments

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

22 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

Related Questions

Multiple Cars not working 1 Answer

using A* pathfinding to go to player after raycast has seen it - c# 0 Answers

Mouse Click + Raycast + Colliders 2 Answers

Change the direction of ScreenPointToRay() Ray 1 Answer

How to raycast from mouse position in scene view? 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