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

Issue with detecting ground giving wrong values

Discussion in 'Getting Started' started by k_babb, Oct 1, 2015.

  1. k_babb

    k_babb

    Joined:
    Sep 30, 2015
    Posts:
    5
    Hi Everyone I am new to c# and unity i am trying to create a coins at height levels but if the coin is above a certain height I need to create a platform.

    This is all working well when the ground level is at height 0 so y=0

    but I don't want a flat level so i created a function to return the y value of the floor but it is returning values of -1.86 none of the floor is below 0?

    here is the function so i am feeding if a float of x where the coin will be created on the x axis any help would be great

    Code (CSharp):
    1.  
    2. float DisFromGround(float ob)
    3.     {
    4.         int l = LayerMask.GetMask("Ground");
    5.         float distanceToGround;
    6.          Vector2 pos=  new Vector2 (ob,20);
    7.         var hit= Physics2D.Raycast(pos, -Vector3.up,20,l);
    8.      
    9.              distanceToGround = hit.transform.position.y;
    10.             Debug.Log(hit.transform.position+" hit pos");
    11.             Debug.DrawLine(pos, hit.point, Color.red, 1);
    12.      
    13.         return distanceToGround;
    14.    
    15.     }
     
    Last edited: Oct 1, 2015
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I tried looking at this last night when I couldn't sleep, and couldn't wrap my head around what you were doing. I figured I must've just been tired... But looking at it again today, I'm still a bit confused.

    In particular, why are you passing a Vector3 to the Physics2D.Raycast function? And does this not throw an error?

    Anyway, Physics2D.Raycast returns a Physics2D struct. That struct contains a transform property that gives the transform of the object that was hit, but not necessarily the point that it was hit at. I'm guessing that you want to actually be accessing the hit.point property, which is a Vector2 you can just grab the Y of.
     
    k_babb likes this.
  3. k_babb

    k_babb

    Joined:
    Sep 30, 2015
    Posts:
    5
    Hi Schneider21

    It does not throw an exception but i did notice since posting that i was using a vector3 mainly because i copied a pasted from another example . i will try what you have suggested and let you know thanks for the advice
     
  4. k_babb

    k_babb

    Joined:
    Sep 30, 2015
    Posts:
    5
    Hi Schneider21

    Thanks for the advice that is now working a treat. Sometime when you are looking at a issue its really experience you need which I am lacking so i appreciate your help on this
     
  5. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    To clarify, is it supposed to be a Vector3? I assume so or your code wouldn't even compile.

    The rest of his suggestion, about 'point' instead of 'transform', sounds like that was the answer.
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    According to the docs, it should be passed a Vector2. So I don't know how it was working with a Vector3 in there...

    But glad to hear it's working for you now!