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

Ray casting multiple times to detect ground

Discussion in 'Physics' started by irjason, Oct 17, 2015.

  1. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Edit - Solved -
     
    Last edited: Oct 17, 2015
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there,

    I don't see anything problematic. How about just using for loops? There's nothing more to it I think. Just split the width of bounds to equal parts and cast a ray on those locations.
     
  3. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    thanks for the reply i have kinda figured it out now on how i should go about doing it, and don't think it should be that resource heavy either.

    Code (CSharp):
    1. [RequireComponent(typeof(BoxCollider2D))]
    2.  
    3.     public class BlockSettings : MonoBehaviour
    4.     {
    5.         /// <summary>
    6.         /// Ediable
    7.         /// </summary>
    8.         [Tooltip("If The Object Is Grounded.")]
    9.         public bool
    10.             blockGrounded;
    11.         [Tooltip("Masking Layer for ground.")]
    12.         [SerializeField]
    13.         private LayerMask
    14.             m_WhatIsGround;
    15.         [Tooltip("The distrance to be increased for longer ray checking.")]
    16.         [SerializeField]
    17.         [Range(0.0f, 1.0f)]
    18.         private float
    19.             distanceToGroundIncrease = 0.1f;
    20.         [Tooltip("Amount of rays to cast and space out.")]
    21.         [SerializeField]
    22.         [Range(0.0f, 10.0f)]
    23.         private int
    24.             raysAmount = 5;
    25.         [Tooltip("The Offset amount for the rays.")]
    26.         [Range(0.0f, 1.0f)]
    27.         public float
    28.             RayOffset = 0.05f;
    29.  
    30.         /// <summary>
    31.         /// Private      
    32.         /// </summary>
    33.         private float distToGround;
    34.         private Rect rayBoundsRect;
    35.         private BoxCollider2D _boxColl;
    36.  
    37.         void Awake ()
    38.         {
    39.             distToGround = gameObject.GetComponent<Collider2D> ().bounds.extents.y;
    40.             _boxColl = gameObject.GetComponent<BoxCollider2D> ();
    41.  
    42.             RaysParams ();
    43.         }
    44.  
    45.         void FixedUpdate ()
    46.         {
    47.             //Set the left and right ray pos
    48.             Vector2 CastFromLeft = new Vector2 (rayBoundsRect.xMin,
    49.                                                         rayBoundsRect.center.y + RayOffset);  
    50.             Vector2 CastToRight = new Vector2 (rayBoundsRect.xMax,
    51.                                                        rayBoundsRect.center.y + RayOffset);      
    52.  
    53.             RaycastHit2D[] hitsStorage = new RaycastHit2D[raysAmount];
    54.  
    55.             for (int i = 0; i < raysAmount; i++) {  
    56.                 Vector2 rayOriginPoint = Vector2.Lerp (CastFromLeft, CastToRight, (float)i / (float)(raysAmount - 1));
    57.  
    58.                 hitsStorage [i] = HeroTools.HeroRayCast (rayOriginPoint, -Vector2.up, distToGround + distanceToGroundIncrease, m_WhatIsGround, true, Color.blue);  
    59.                 if (hitsStorage [i]) {
    60.                     blockGrounded = true;
    61.                 } else {
    62.                     blockGrounded = false;
    63.                 }
    64.             }
    65.         }
    66.  
    67.         void LateUpdate ()
    68.         {
    69.             //Update bounds pos
    70.             RaysParams ();
    71.         }
    72.  
    73.         void RaysParams ()
    74.         {      
    75.            
    76.             rayBoundsRect = new Rect (_boxColl.bounds.min.x,
    77.                                            _boxColl.bounds.min.y,
    78.                                            _boxColl.bounds.size.x,
    79.                                            _boxColl.bounds.size.y);  
    80.  
    81.             //Debug test..
    82.             /*
    83.                 Debug.DrawLine (new Vector2 (_rayBoundsRectangle.center.x, _rayBoundsRectangle.yMin), new Vector2 (_rayBoundsRectangle.center.x, _rayBoundsRectangle.yMax), Color.red);
    84.                 Debug.DrawLine (new Vector2 (_rayBoundsRectangle.xMin, _rayBoundsRectangle.center.y), new Vector2 (_rayBoundsRectangle.xMax, _rayBoundsRectangle.center.y), Color.white);
    85.             */
    86.         }
    87.     }
     
    Last edited: Oct 17, 2015