Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a 2D Laser Beam

Discussion in '2D' started by MoonJellyGames, Oct 16, 2014.

  1. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Most of the results I get for creating laser beams describe how to make Star Wars-like projectiles. That's not what I'm going for. Instead, I need a constant beam that stops when it hits (most) collider surfaces. This Live Training video is a great example of what I'm going for, except that my lasers are coming from traps in the environment, so they aren't anchored to the player; most of them are firing constantly instead of on input, and of course, mine are in a 2D environment:


    Here is the code used in his example:

    Code (CSharp):
    1. IEnumerator FireLaser()
    2. {
    3.      line.enabled = true;
    4.      while (Input.GetButton("Fire1"))
    5.      {
    6.           Ray ray = new Ray(transform.position, transform.forward);
    7.           RaycastHit hit;
    8.  
    9.           line.SetPosition(0, ray.origin);
    10.  
    11.           if (Physics.Raycast(ray, out hit, 100))
    12.                line.SetPosition(1, hit.point);
    13.           else
    14.                 line.SetPosition(1, ray.GetPoint(100));
    15.  
    16.           yield return null;
    17.      }
    18.  
    19.      line.enabled = false;
    20.  
    21.      }
    22. }
    23.  
    The documentation describes the LineRenderer as "The line renderer is used to draw free-floating lines in 3D space.", however since a Vector2 can be cast to a Vector3, that doesn't seem to be an issue.

    Here is my code:

    Code (CSharp):
    1.     IEnumerator FireLaser()
    2.     {
    3.         line.enabled = true;
    4.  
    5.         while (laserOn)
    6.         {
    7.             Ray2D ray = new Ray2D(transform.position, transform.right);
    8.             RaycastHit2D hit;
    9.  
    10.             line.SetPosition(0, ray.origin);
    11.  
    12.             if (Physics2D.Raycast(ray, out hit, distance))
    13.                 line.SetPosition(1, hit.point);
    14.             else
    15.                 line.SetPosition(1, ray.GetPoint(distance));
    16.  
    17.             yield return null;
    18.         }
    19.         line.enabled = false;
    20.     }
    I'm getting two errors, both at the line that does the line that does the raycast:

    The best overloaded method match for `UnityEngine.Physics2D.Raycast(UnityEngine.Vector2, UnityEngine.Vector2, float)' has some invalid arguments

    Argument `#1' cannot convert `UnityEngine.Ray2D' expression to type `UnityEngine.Vector2'


    So the normal (3D) Raycast has no trouble converting a 3D Ray to a Vector3, but the 2D Raycast can't seem to do the same for a Ray2D.

    Did I make a mistake or is there a different way of going about this?
     
  2. DricoJD

    DricoJD

    Joined:
    Sep 3, 2012
    Posts:
    15
    In the few months that we have had 2D features I have never had to learn or come across Ray2D and I would think that sooner or later there will be a tutorial by Unity on how to do this, but I am not going to have no idea on how to sort your code out, but I can over some troubleshooting tips from what it might be.

    -A vector 2 requires and x and a y vaule make sure that they are in the correct place by checking the reference.

    -Lastly RaycastHit2D is a function I think you should be using to cast http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

    But Like I said I have never come across it and will need to get learning soon, so can you keep me updated on the things you learn in case I need it some day!
     
  3. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Okay, I got it working. I've used 2D Raycasts before, so I just did it the way that I normally would, and it came together. :)

    Code (CSharp):
    1.     IEnumerator FireLaser()
    2.     {
    3.         line.enabled = true;
    4.  
    5.         while (laserOn)
    6.         {
    7.             Ray2D ray = new Ray2D(transform.position, transform.right);
    8.             RaycastHit2D hit;
    9.  
    10.             line.SetPosition(0, ray.origin);
    11.  
    12.             hit = Physics2D.Raycast(ray.origin, Vector2.right, distance);
    13.  
    14.             if (hit.collider)
    15.             {
    16.                 line.SetPosition(1, hit.point);
    17.             }
    18.             else
    19.                 line.SetPosition(1, ray.GetPoint(distance));
    20.  
    21.             yield return null;
    22.         }
    23.         line.enabled = false;
    24.     }
    The first two arguments in the Raycast code (in the example) still don't make any sense to me though. :/
     
    KurtRussellsMullet likes this.
  4. DricoJD

    DricoJD

    Joined:
    Sep 3, 2012
    Posts:
    15
    If it works it works, half the time the code I right makes some sense, but other times I have no idea what something's are, for example matrix but when it works it works
     
  5. BeefSupreme

    BeefSupreme

    Joined:
    Aug 11, 2014
    Posts:
    279
    You mean the Raycast origin and direction?
     
    DricoJD likes this.
  6. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Ah, yes. I didn't realize that there are multiple ways to do a raycast, each with different arguments. I thought that (in the example), a Ray was being used as an argument where a vector should be.