In this lecture, we're going to add a little bit of extra code to make the cars turn away from each
other if there's a collision.
And this code will help cars pass each other as well.
So what we're going to do is take the capsule point or our tracker point that we're trying to drive
to and move it by a certain offset if we detect a collision.
So let's take the car that's behind in this case.
When it runs into the car in front of us, it's going to trigger a collision in both cars.
Script on both cars is going to recalculate a new goal position that is based on the tracker position,
plus a bit of an offset.
And what it's going to do is going to make one car turn in one direction and the other car turn in the
other direction, which will give you some really interesting avoidance dynamics with relatively simple
code.
And what we're going to do is use the colliders and the rigid body that's already on the car.
Okay.
So let's add in the code.
To do this, you'll need a script called a void detector, and you can see how it's spilt over here.
So it's just adding a C-sharp script to your project.
Let's open that up and add in the code.
So at the top, we're going to add in a public.
Float.
Avoid path.
Equals zero.
Public float.
Avoid time.
Equals zero.
And these are settings that we are going to use from elsewhere.
But you can also set in the inspector.
Then we want a public float for wander distance equals zero.
And that's going to be how far we can actually wander away from the path.
Actually, let's not make that zero.
Let's make that for and public float for a void length, which is how long we want to keep avoiding.
Four.
So this is the time that we want to go off on our avoiding path.
Now we're not going to use a start and update in these.
We're going to use trigger values to determine when we should move.
So we want void on trigger exit collider call and we're going to basically figure out when two cars
have come within triggering distance of each other and then use that to actually put some avoiding code
in.
So collect game object tag does not equal car then we will turn and if it does, then we'll go avoid
time equals zero.
So we're actually setting the avoid time back to zero if we're exiting a sort of a collision or being
in the road of someone.
Now the actual code is going to happen for avoiding when we're in the trigger distance.
So on trigger stay collider call and let's put that other E out of there.
Okay.
And then we're going to go if called game object tag.
It's not a car.
Of course we're checking that we've got a tag of car so that we're only checking we collide with cars.
In order for this script to work, for example, we don't want other things to sort of trigger.
For example, there's any other triggers that you might put into your environment and then we'll go
rigid body for the other car equals called get component, rigid body avoid time equals time time which
is the time now plus the avoid length.
Then we will create a vector three, which will be the other car local target.
So where the other car is, with respect to the current car, it's going to equal transform inverse
transform point.
Again, it needs to be relative not in world coordinates.
Other car.
Dot game object.
Dot transform.
Dot position.
Float.
Other car angle.
The angle to the other car of course.
So this will be our math f dot a10 two.
Same angle code we've used for moving around our waypoints and calculating those other.
CA.
Local target and it'll be dot x and then other local target dot z.
So same as our progress tracker, our waypoints.
When we were looking at the angle of which way, which direction we were going and where it is now,
we're doing that with the cars.
Okay.
And then we're going to set our void path equals wander distance multiplied by negative math f sine
other car angle.
So we're actually taking the negative sine of the angle to the other car so that we turn by the same
amount of angle in the other direction, so away from the other car.
Okay, so that's all we need in here.
We're going to save this and we're going to go back into unity.
We're going to select our car because we need to put this avoid script onto it.
Okay.
And we're going to grab and put a void detector onto there.
Okay.
So here's our values here.
The wander distance and the avoid length of the things that you can change and play around with.
The other things are actually used inside our controller script, so open up that.
And what we're going to do is at the top and you might have seen these in the previous video as lines
of code that weren't necessary yet, this one here.
So we want to get a hold of our voice detector script on this particular car and when to keep a copy
of it so that we're not having to get it all the time.
And then inside of our start, we will actually set it and we'll get the avoid detector that is on this
particular game object.
All right.
So we're going to now use this down inside of our update.
So if we come down into the update, you'll see the code already in here.
So I've moved things around just a little bit.
All I've done here at the top is a vector three local target is no longer set up here.
It gets set based on the avoidance time.
If there is no avoidance time needing to happen, then we've got the value that we had before.
Otherwise we're going to create a different local target which includes our avoid path using the trackers
right direction.
So sideways movement multiplied by the avoid path.
Or it actually just does what the diagram at the beginning of this lecture does.
It just moves the tracker one way or the other, depending on where we want to go next.
Okay.
So then the target angle which we are calculating up the top, I've also moved that down into here below
this code because the local target is what's used for calculating this target angle.
Okay, now this distance to target, we took out in the previous lecture that I had still hanging around
in there.
You don't need it.
Obviously we're not using it for anything.
Of course you could comment it out at this moment.
You might want to use it later on, put it back as another one of your variables, but it's not necessary.
Okay, so this is the changes.
Let's save this now.
We'll go back into unity.
Now our car needs to have a trigger on it so that it can actually trigger other cars if you select it.
At the moment, it's got a collider on it, which is very nice and very tightly bound to the car.
You don't want to use this because by the time this collider and another car's version of this collider
have hit each other, cars have actually hit each other.
So we want a bit of a buffer sitting around this car, and we don't want that buffer to actually be
a physics object as far as a rigid body and or cause sort of physical collisions.
We just want to as a trigger.
So select the car and then over in your inspector, we're going to go down the bottom here and we're
going to add a box collider so do that.
Then in the scene we need to find where that goes and it's going to go like down here.
Okay.
So it sits basically on the origin of the car.
You have to align this up with the car basically.
So let's move it up.
It only sort of really needs to be around the body and wherever other cars are going to hit it.
So let's have a look.
We're going to move the center of this box collider up into the center of the car.
Then the size of it we can grab the Z value and if you start dragging that, eventually it will pop
out both ends of the actual car.
And you can see that coming through now there for me.
Okay.
Now, the fact that it's no higher than this wintergreen value is kind of okay for me because no other
car is going to sort of hit it up there.
Now, how close do you want other cars to be able to get to cars before they decide that they're going
to swerve out of the way?
Well, that will depend on what your Z value is here, how much of a room around it.
The other one you'll want is probably a bit of side buffering as well, so that they don't get to close
together.
And that's another thing you can kind of play around with if you want.
Now, once you've got that, you're going to go over and set is trigger on that box.
Collider So that it will use those trigger methods that we just coded in.
Finally, you want to go up to the inspector at the top and you want to tag your car as car.
If there is no car in your list, you want to go to add tag add in the tag car.
We used lowercase car.
Make sure you do that, then go back to the car and make sure you select car for its tag.
Okay, well, we've done it right.
That's it.
So that is one car.
Now, at this point, it's probably a good idea to save your project.
I keep having issues with mine crashing, which I do believe is my video, my screen capture software.
But it does cause inconveniences.
Right.
So let's add another car.
So select that car control dx that car.
And in the scene, we're going to hit the key and move that other car out.
Okay, so there it is.
There now this other car is an exact copy of our car over here, which means it's going to have the
camera attached to it.
So make sure you go in that second car and remove the camera because you only really want the camera
on this one.
Or you could take it off altogether and not have it on any car.
It depends what you want to do with this.
So there's one car and let's control D this second car because it doesn't have the camera and then we
can use the W to move that over here.
Control D.
And you can add as many cars as you like at this point.
You can press play and have a look at what you get.
I will tell you, it's not going to be perfect.
In fact, we're going to come back in the next lecture and look at evaluating what we've done.