How to create Path considering size of colliders of moving objects?

Hello. This is my first post. I heard a lot of good things about this Project and I want to try. I have created 2d Scene. Created maze of walls with BoxColliders2D. Then I have created vehicle sprite with CircleCollider2D and with Seeker.
After that I created A* GameObject with AstarPath component. Setted width, height and rotated by X axis to 90 degrees. Ticked Use 2d Physics and in collision Testing is set to Ray. At last I have made scan.

My Vehicles code:
void Start () { seeker = GetComponent<Seeker>(); }

Inside FixedUpdate:
GameObject enemy = findClosestEnemy(); seeker.StartPath(transform.position, enemy.transform.position, OnPathComplete);

OnPathComplete will get new Path and assign it to my private variable. To move it I use this:
`
if (currentPath == null)
return;

if (currentWayPoint >= currentPath.vectorPath.Count)
return;

Vector3 newPosition = currentPath.vectorPath[currentWayPoint];

float angle = Mathf.Atan2(newPosition.y - transform.position.y, newPosition.x - transform.position.x) * Mathf.Rad2Deg;

transform.position = Vector3.MoveTowards(transform.position, newPosition, Time.fixedDeltaTime * 2);

Quaternion newRotation = Quaternion.Euler(new Vector3(0, 0, angle));

transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.fixedDeltaTime * 2);

if (Vector3.Distance(transform.position, currentPath.vectorPath[currentWayPoint]) < nextWaypointDistance)
{
currentWayPoint++;
return;
}
`

As you can see, I have used code from Getting Started. Now when I start my scene, my vehicle creates Path and starts to move, but this Path is not considering own width(radius) of CircleCollider2D and it tries to pass near the wall making its movement impossible. What did I need to add to make my vehicle to consider own colliders size when drawing Path?

collision Testing is set to Ray
There's your problem. Don't set it to ray. Set it to Sphere (which for 2D is a circle).

You can also use erosion to get roughly the same effect (but using Sphere checking is faster).

1 Like