Trying to figure out how to properly use my CircleCast Detection in my 2D Prototype
Hey All,
Been trying to work on this all week and I've not really had answers on Discord so first time posting here!
What I am trying to do is recreate a Homing Attack System in Unity similar to Sonic The Hedgehog and for the most part I have working! But I am trying to get it so I can't target through the walls.
Below is the CircleCast I am using in my code
RaycastHit2D hit = Physics2D.CircleCast(targetPoint.position, 3f, -transform.right, 10f);
I have tried adding the filter at the end for my Enemies which end up in me just being able to Pass through walls. With the current implmentation It targets everything other than my Enemy and I am unsure how to properly set it up. I have pasted my code below.
RaycastHit2D hit = Physics2D.CircleCast(targetPoint.position, 3f, -transform.right, 10f);
if (hit.collider != null && hit.collider.tag == "Enemy")
{
Debug.Log(hit.collider.name);
/* distance = Vector3.Distance(transform.position, hit.transform.position);
if (closestObejct != null)
{
float currentDist = Vector3.Distance(closestObejct.position, hit.transform.position);
if (currentDist > distance)
{
if (closestObejct.Find("Target(Clone)").gameObject != null)
Destroy(closestObejct.Find("Target(Clone)").gameObject);
gcs.currentTarget = hit.transform.gameObject;
closestObejct = hit.transform;
}
}
if (closestObejct == null)
{
closestObejct = hit.transform;
gcs.currentTarget = hit.transform.gameObject;
}*/
}
else if (hit.collider != null && hit.transform.gameObject.layer != whatIsEnemy)
{
Debug.Log("No target");
}
}
Does anyone know how I can make it work?
Comment