So finally i finished the Obstacle avoidance steering behavior. The image below shows the initial setup.
The small green sphere will act as the avoidance actor and all other spheres will be the Obstacles.
The image above displays the Debug Lines. The pink line is the velocity vector and the black rectangle is basically what is used for collision avoidance. I am using two Raycasts of the length of rectangle to detect collision. I drew the rectangle using the Line Renderer component of Unity.
I have used three line Renderers to draw the three lines of the rectangle. Unity does a bad job of drawing more than one line using the same line Renderer. Hence this approach. Cross Product is taken between the Vector coming out of the screen which is Vector3.back in Unity and velocity vector which then is normalized. To get the origin point on the sphere's circumference Cross Product is multiplied with Renderer.extent value which is 0.64f in this case.(Avoid magic numbers). This rectangle is going to shrink and expand depending on the velocity of the actor and time which will be a constant value. The rectangle basically denotes how far in future we need to see in order to detect collision. Also depending on where the collision occurred how much steering or braking should be applied to avoid collision.
Next check is for Collision using Raycasts. The Raycasts are the same length as that of the Line Renderers. I am checking which of the two raycasts will collide first and then assigning the hit point. This is important to do as both the raycasts can hit obstacles at the same time and the actor won't give the desired behavior. Physcis.Raycasts is a handy function in this case.
Next, I calculate the allignedVector which will be the dot product of vector where the collision occurred and velocity vector. Collision Distance Ratio is basically the value on the scale of 0-1 where 1 is the center of our actor and 1 the end of rectangle. This tells us where exactly the collision occurred and will be useful in calculating the steering acceleration and braking acceleration.
Now to steering acceleration which will be applied in the laterally to the actor, perpendicular to the velocity vector. Here equation between liner weight and distance will be [w = 1 - d] which is a linear increase. If collision is detected earlier than less steering acceleration will be applied as to when the collision is detected late.
Next Braking Acceleration is calculated which increases quadratically with respect to the distance [b = (1 - d)^2]. It means when the collision is detected late the actor should brake hard. Both these values are returned and added to give the final acceleration.
-Ajinkya