Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to create a laser?

Discussion in '2D' started by SebGM2019, Mar 7, 2020.

  1. SebGM2019

    SebGM2019

    Joined:
    Aug 3, 2018
    Posts:
    34
    Hi guys, I'm trying to create an infinite laser in Unity 2d. Either using raycasts or prefabs. I've tried using this video:
    and this link: https://steredenn-game.tumblr.com/post/98397504410/steredenn-making-an-expandable-laser

    But none of them have worked (In the first one the raycast didn't show the line, and in the second one my prefabs were pretty buggy, and didn't show in the right angle.

    My goal is to achieve an infinite laser that adapts to angles and collides with objects
    As the gif shows, but should shorten whenever is colliding with something
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    u need math and trig
     
  3. SebGM2019

    SebGM2019

    Joined:
    Aug 3, 2018
    Posts:
    34
    Could you be a little more specific?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    You should be able to use RaycastHit2D to help you with this. When you cast a ray you can get out the "hit" information. In fact, the RaycastHit.normal (not 2D) example shows an example that's highly similar to the problem you want to solve. Try that in 2D and you're off to the races.
     
  5. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    u would also need to account for convex and non convex surface, decide how many laser bounces you require and handle laser hitting itself, is non trivial solution but most PTs use this in 3d space so possible.