- Home /
New waypoint if linecast detects obstacle?
Hey guys, been doing some research and thought I'd ask on here. I'm trying to make my racing game ai a little bit more intelligent by adding some form of obstacle avoidance. The script I have is heavily based on a youtube tutorial but it essentially has the AI go form point to point. What I've managed to add is this...
void Update()
{
//RaycastHit hit;
RaycastHit hit = new RaycastHit();
for(int n = 0; n < nodes.Length; n++)
{
Debug.DrawLine(nodes[n].position - Vector3.down, nodes[(n+1)%nodes.Length].position - Vector3.down, new Color(1,1,1,0.2f));
if (Physics.Linecast (nodes[n].position, nodes[(n+1)%nodes.Length].position,out hit)) {
print("Player detected"+nodes[n]);
Debug.DrawLine(nodes[n].position, nodes[(n+1)%nodes.Length].position, Color.green);
print (hit.point);
}
}
}
So what the above script does is draw linecasts between each waypoint. What I was hoping to implement was a sort of auxiliary waypoint. So a waypoint that would be created at just outside the point of disturbance allowing it so go around any obstacles basically.
My issue now is that When the game starts it adds all the nodes to an array. Would it be possible to add an additional waypoint into the array at specific point and then get the waypoint order to be rewritten? So even if the object was mid way through following a waypoint and an obstacle jumped up at the last second it move move to the new waypoint.
I'm thinking of rewriting the script and making it a lot simpler.
Here's a link to the movement script http://pastebin.com/gpeqjGaP
but more importantly the waypoint following script.
I guess what I'm hoping for are some pointers for what I can change to make this possible or even if I should just start a new script...
I was thinking about instead having a raycast on the ai object and having some script that would maybe have the ship move out of the way.
Not too sure which would be better.
Thanks in advance guys :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613094814im_/https://answers.unity.com/themes/thub/images/avi.jpg)