var grounded : boolean = false;
var groundCheck : Transform;
var groundRadius : float = 0.2;
var whatIsGround : LayerMask;
function Awake()
{
groundCheck = transform.Find("groundCheck");
}
//Only Update changes in both scripts.
function Update()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
}
or replace the Update part with
if(Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);)
{
grounded = true;
}
Yes, they will function identically. The second one is just a bit redundant.
if( Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround) != null)
{
grounded = true;
}
As mentioned in the documentation for Physics2D.OverlapCircle, this returns a collider and hence allocates memory, so if you just need to know how many objects are colliding with the sphere, you may wanna use Physics2d.OverlapCircleNonAlloc
out Collider2D[] results;
if( Physics2D. OverlapCircleNonAlloc(groundCheck.position, groundRadius, results, whatIsGround) > 0)
{
grounded = true;
}