Search Unity

Raycast2D errors.

Discussion in 'Scripting' started by pixelfixation, Jul 30, 2014.

  1. pixelfixation

    pixelfixation

    Joined:
    Feb 7, 2014
    Posts:
    29
    Hey guys i'm working on the "Fun with lasers" live training archive by mike gieg. However i am doing it in 2d for my game. this is what i have.

    Code (CSharp):
    1.  
    2.             line1.enabled = true;
    3.             line2.enabled = true;
    4.  
    5.             Ray2D ray1 = new Ray2D(line1.transform.position, line1.transform.up);
    6.             Ray2D ray2 = new Ray2D(line2.transform.position, line2.transform.up);
    7.             RaycastHit2D hit1;
    8.             RaycastHit2D hit2;
    9.  
    10.             line1.SetPosition (0, ray1.origin);
    11.             if(Physics2D.Raycast(ray1, out hit1, 50f))
    12.                 line1.SetPosition(1, hit1.point);
    13.             else
    14.                 line1.SetPosition (1, ray1.GetPoint(50f));
    15.  
    16.             line2.SetPosition (0, ray2.origin);
    17.             if(Physics2D.Raycast (ray2, out hit2, 50f))
    18.                 line2.SetPosition(1, hit2.point);
    19.             else
    20.                 line2.SetPosition (1, ray2.GetPoint(50f));
    i have an error on the 2 if statements that says i can't convert a ray2D to a vector2D.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Physics.Raycast(...) has an overload function with these parameters:

    Physics2D.Raycast(...) only has a single function with these parameters:

    http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
    http://docs.unity3d.com/ScriptReference/Physics.Raycast.html


    You are going to have to use the origin/direction (i.e. the parameters you're passing to form the ray) rather than a ray in the raycast.
     
  3. pixelfixation

    pixelfixation

    Joined:
    Feb 7, 2014
    Posts:
    29
    Thanks man i really really appreciate it.