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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics2D.Raycast doesn't work on rigidbody2D

Discussion in '2D' started by thooom, May 11, 2014.

  1. thooom

    thooom

    Joined:
    Apr 29, 2014
    Posts:
    11
    I am creating a small application where I have some cars driving around in a top-down view environment, and I am trying to give them some AI so they will brake if they are about to hit another car. At first I had some problems getting the Raycast to not hit itself, but eventually I got it working. However, now it won't react to other cars. I am using a prefab that clones up the same car every X seconds but they don't seem to notice eachother. The code I am using to notice the cars are the following:

    Code (csharp):
    1. function Update ()
    2. {
    3.     somethingSpotted = Physics2D.Raycast(Vector2(rigidbody2D.transform.position.x,rigidbody2D.transform.position.y-1.2),Vector2(0.5,-4.5));
    4.     Debug.DrawLine(Vector2(rigidbody2D.transform.position.x,rigidbody2D.transform.position.y-1.2),Vector2(0.5,-4.5),Color.blue);
    5.    
    6.     if (somethingSpotted == true)
    7.     {
    8.         Debug.Log("Car spotted");
    9.         rigidbody2D.velocity = new Vector2(0,0);
    10.     }
    11. }
    Something spotted is a boolean that is false by default. I have a prefab for the car model with both a box collider and a rigidbody2D. But the Raycast never seems to be true and I can't figure out why.

    PS: The Vector2 is set to one of the cars spawn points for debugging purposes.
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    What jumps out at me is that you're using the same parameters for Raycast as DrawLine, but they function differently. Raycast wants start (world), direction (local), and distance (assumed infinite). DrawLine wants start (world) and end (world). Your Raycast isn't going where your DrawLine is going. Maybe you should be using Physics2D.Linecast?
     
  3. thooom

    thooom

    Joined:
    Apr 29, 2014
    Posts:
    11
    That could be a good idea. Or I could change it to raycast :D Thanks for the reply. Also, is there a way to create a Debug that is equivalent to Raycast? I'd prefer to use direction/distance rather than a start/end point
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Vector math is easy:

    Vector2 EndPoint = StartingPoint + (Direction.Normalized() * Distance);
     
  5. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    One other thing: Physics2D.Raycast does not return a boolean value, it returns an instance of RaycastHit2D which by the way always returns a non-null value. So, in order to check if your ray cast hit something, you have to test if hit.collider is not null. Please note that the code snippet in the documentation is not correct and misleading, and it would be nice if it was updated sometime soon.
     
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    ...Which BTW can be implicitly cast to a Boolean, internally checking whether or not there's a hit. Seems more robust than checking the collider, given that the implementation already doesn't match the documentation.
     
  7. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Interesting. I never tried that actually. Thanks for pointing that out.
     
  8. thooom

    thooom

    Joined:
    Apr 29, 2014
    Posts:
    11
    Is there a way to get distance to hitpoint with Linecast though?