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

Please convert RaycastHit2D to RaycastHit 3d in unity c#..

Discussion in 'Scripting' started by babji3, Oct 10, 2015.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi guys,
    I need help in this line ,how i convert this to 3d, here it is

    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(rayOrigin,Vector2.up*directionY, rayLength, collisionMask);
    2. if(hit)
    3. {
    4. velocity.y = (hit.distance - skinWidth)*directionY;
    5. rayLength = hit.distance;
    6. }
    plz convert this line for 3d raycasst.
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Im not exactly sure what you mean by convert. Do you mean something like this?
    Code (CSharp):
    1.         RaycastHit hitInfo;
    2.         if(Physics.Raycast(rayOrigin, Vector3.up * directionY, out hitInfo, rayLength, collisionMask))
    3.         {
    4.             velocity.y = (hitInfo.distance - skinWidth) * directionY;
    5.             rayLength = hitInfo.distance;
    6.         }
     
    Kiwasi likes this.
  3. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Ya like this only, but here why if condition came?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The if condition is to see if the raycast hits anything. No point trying to do something with the result if it's null.