Edit - Solved -
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.
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.
[RequireComponent(typeof(BoxCollider2D))]
public class BlockSettings : MonoBehaviour
{
/// <summary>
/// Ediable
/// </summary>
[Tooltip("If The Object Is Grounded.")]
public bool
blockGrounded;
[Tooltip("Masking Layer for ground.")]
[SerializeField]
private LayerMask
m_WhatIsGround;
[Tooltip("The distrance to be increased for longer ray checking.")]
[SerializeField]
[Range(0.0f, 1.0f)]
private float
distanceToGroundIncrease = 0.1f;
[Tooltip("Amount of rays to cast and space out.")]
[SerializeField]
[Range(0.0f, 10.0f)]
private int
raysAmount = 5;
[Tooltip("The Offset amount for the rays.")]
[Range(0.0f, 1.0f)]
public float
RayOffset = 0.05f;
/// <summary>
/// Private
/// </summary>
private float distToGround;
private Rect rayBoundsRect;
private BoxCollider2D _boxColl;
void Awake ()
{
distToGround = gameObject.GetComponent<Collider2D> ().bounds.extents.y;
_boxColl = gameObject.GetComponent<BoxCollider2D> ();
RaysParams ();
}
void FixedUpdate ()
{
//Set the left and right ray pos
Vector2 CastFromLeft = new Vector2 (rayBoundsRect.xMin,
rayBoundsRect.center.y + RayOffset);
Vector2 CastToRight = new Vector2 (rayBoundsRect.xMax,
rayBoundsRect.center.y + RayOffset);
RaycastHit2D[] hitsStorage = new RaycastHit2D[raysAmount];
for (int i = 0; i < raysAmount; i++) {
Vector2 rayOriginPoint = Vector2.Lerp (CastFromLeft, CastToRight, (float)i / (float)(raysAmount - 1));
hitsStorage [i] = HeroTools.HeroRayCast (rayOriginPoint, -Vector2.up, distToGround + distanceToGroundIncrease, m_WhatIsGround, true, Color.blue);
if (hitsStorage [i]) {
blockGrounded = true;
} else {
blockGrounded = false;
}
}
}
void LateUpdate ()
{
//Update bounds pos
RaysParams ();
}
void RaysParams ()
{
rayBoundsRect = new Rect (_boxColl.bounds.min.x,
_boxColl.bounds.min.y,
_boxColl.bounds.size.x,
_boxColl.bounds.size.y);
//Debug test..
/*
Debug.DrawLine (new Vector2 (_rayBoundsRectangle.center.x, _rayBoundsRectangle.yMin), new Vector2 (_rayBoundsRectangle.center.x, _rayBoundsRectangle.yMax), Color.red);
Debug.DrawLine (new Vector2 (_rayBoundsRectangle.xMin, _rayBoundsRectangle.center.y), new Vector2 (_rayBoundsRectangle.xMax, _rayBoundsRectangle.center.y), Color.white);
*/
}
}