Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Major_Lag · Jan 11, 2020 at 07:53 AM · scripting problem2d gamereflectionraycasthit2dlaser beam

Help with reflecting in 2D.

I've been trying to create a little lasers and mirrors with the goal being get a laser past obstacles to a receiver and have been trying to get the reflecting down. To help I put a line gizmo to help me visualize things so i can just slap on a line renderer once I iron it out. I've been trying to do it recursively and i'm sure i'm close but i'm struggling to figure it out. Here is what i got:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MyLaser3 : MonoBehaviour
 {
     public int maxReflectionCount = 5;
     public float maxStepDistance = 200;
 
     private void OnDrawGizmos()
     {
         drawPredictedReflection(this.transform.position, this.transform.up, maxReflectionCount);
     }
 
     void drawPredictedReflection(Vector2 position, Vector2 direction, int reflectionsRemaining)
     {
         if (reflectionsRemaining <= 0)
         {
             return;
         }
 
         Vector2 startingPos = position;
 
         RaycastHit2D hit2D = Physics2D.Raycast(startingPos, this.transform.up, maxStepDistance);
 
         if (hit2D) //if we hit somthing reflect, else just keep going
         {
             direction = Vector2.Reflect(direction, hit2D.normal);
             position = hit2D.point;
             Debug.Log(direction);
         }
         else
         {
             position += direction * maxStepDistance;
         }
 
         Gizmos.color = Color.yellow;
         Gizmos.DrawLine(startingPos, position);
 
         drawPredictedReflection(position, direction, reflectionsRemaining - 1);
 
 
     }
 }
 

Let me know your thoughts/improvements or if i'm way off and to try a different way.

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
2
Best Answer

Answer by TreyH · Jan 11, 2020 at 02:27 PM

Your math looks and ended up being fine. One issue you might've bumped into was your use of:

 position = hit2D.point;


for assigning the next ray source. This seems fine and it's right geometrically, but you end up with weird behavior from firing a new ray from the surface of a collider that it's also allowed to strike. You'll get a weird floating point situation where sometimes it will hit it and sometimes it won't.

In your case, you'd expect that you won't. To adjust for that, I added a portion of the next ray's direction to its starting position. This fixed the clipping issue and got the expected behavior:

alt text


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RayReflect : MonoBehaviour
 {
     public int maxReflectionCount = 5;
     public float maxStepDistance = 200;
 
     void OnDrawGizmos()
     {
         if (!Application.isPlaying)
             return;
 
         var source = this.transform.position;
         var xyPlane = new Plane(Vector3.forward, source);
 
         var cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (xyPlane.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out float enter))
         {
             var worldCursor = cameraRay.GetPoint(enter);
             var direction = (worldCursor - source).normalized;
 
             this.DrawPredictedReflection(source, direction, this.maxReflectionCount);
         }
     }
 
     void DrawPredictedReflection (Vector2 position, Vector2 direction, int reflectionsRemaining)
     {
         var gizmoHue = (reflectionsRemaining / (this.maxReflectionCount + 1f);
         Gizmos.color = Color.HSVToRGB(gizmoHue, 1, 1);
 
         RaycastHit2D hit2D = Physics2D.Raycast (position, direction, maxStepDistance);
 
         if (hit2D && hit2D.collider) //if we hit somthing reflect, else just keep going
         {
             Gizmos.DrawLine (position, hit2D.point);
             Gizmos.DrawWireSphere(hit2D.point, 0.25f);
 
             direction = Vector2.Reflect (direction, hit2D.normal);
             position = hit2D.point + direction * 0.01f;
         }
         else
         {
             Gizmos.DrawLine (position, position + direction * maxStepDistance);
             position += direction * maxStepDistance;
         }
 
         Gizmos.color = Color.white;
 
         if (reflectionsRemaining > 0)
             DrawPredictedReflection (position, direction, --reflectionsRemaining);
     }
 }
 


reflect.gif (411.8 kB)
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 Major_Lag · Jan 11, 2020 at 07:20 PM 1
Share

Thank you for this! Your assumption was absolutely correct and and now its working as intended.

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

238 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

Related Questions

Object flashing in and out every frame (because of my script) 0 Answers

How to bounce one object correctly? 1 Answer

Can anyone just read this over? for my 2d runner? 1 Answer

How can I find gameObject child. 0 Answers

Bullet speed changes depending on how close and far I click the mouse button 2 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