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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Errors with Physics2D.Linecast

Discussion in 'Scripting' started by DevAigina, Aug 15, 2018.

  1. DevAigina

    DevAigina

    Joined:
    Jun 6, 2017
    Posts:
    29
    I am trying to check if player is grounded.
    I am trying to solve this with linecast. I am calculating 2 edge positions of collider. (BottomLeft , BottomRight),
    and i am linecasting between this 2 positions , but it isn't working properly.
    Here is the code ;
    Code (CSharp):
    1. private void FixedUpdate()
    2. {
    3.         Vector2 _bottomLeft = new Vector2( m_Collider.bounds.min.x , m_Collider.bounds.min.y );
    4.         Vector2 _bottomRight = new Vector2( m_Collider.bounds.max.x , m_Collider.bounds.min.y );
    5.  
    6.         Debug.DrawRay( _bottomLeft , Vector2.right , Color.yellow );
    7.  
    8.         RaycastHit2D ray = Physics2D.Linecast( _bottomLeft , _bottomRight , m_worldLayer );
    9.  
    10.         if ( ray.collider != null )
    11.         {
    12.             Debug.Log( "Hey !" );              // This is working...
    13.             m_Grounded = true;                 // Not working...
    14.             m_currentCell = ray.collider.GetComponent< Cell >();    // Not working...
    15.         }
    16.         else
    17.         {
    18.             m_Grounded = false;
    19.             m_currentCell = null;
    20.         }
    21. }
    Thanks for any help.
    Edit : It is only printing "Hey !" only once , at this point i am thinking about linecast isn't the solution for this problem.
     
    Last edited: Aug 15, 2018
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Usually if you use Linecast to determine if you are grounded, you would cast a line from your player (possibly starting at their feet) straight down to check if the linecast detects a collider. You are casting a horizontal line, which I am imagining is parallel to the ground.
     
  3. DevAigina

    DevAigina

    Joined:
    Jun 6, 2017
    Posts:
    29
    Yes , i am casting horizontal line (between bottom left and bottom right) is it the reason ? I mean i can still see "Hey !" message on console when i run my scene.
     
  4. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    How do you know line 13 is not working? Add
    Debug.Log (m_Grounded);
    immediately after it and you'll probably see it is true.

    But on line 14, you need to get the component from the transform or gameobject, not the collider.
    m_currentCell = ray.transform.GetComponent<Cell> ();
     
  5. DevAigina

    DevAigina

    Joined:
    Jun 6, 2017
    Posts:
    29
    Hi , i know it because it is public variable so i can see it from Unity's inspector. I tried "Debug.Log( m_Grounded ); " and it is printing "false" just like what i saw in Unity's inspector.
    At this point i am thinking about to use another solution for checking if character is grounded. Maybe something like normal raycasting or OverlapCircle.
     
  6. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Is it possible that it is getting set to false immediately after it is getting set to true, so you cannot see it in the inspector? If you are getting the debug "Hey" statement (and there are no other debug "Hey" statements to get confused by), there's no way it would execute line 12 above without executing line 13 immediately after. Try using a breakpoint to watch what is happening.

    That is another common method, or as I said casting straight down rather that parallel to the ground.
     
    Last edited: Aug 16, 2018
  7. DevAigina

    DevAigina

    Joined:
    Jun 6, 2017
    Posts:
    29
    @barskey Thank you so much for helping out , now i am using simple Physics2D.Raycast instead of "Linecast" it is working fine.