Sticking with the v2 folder, if we use the game from there, you'll see that we've got the yellow blocks. But if we try and walk through one, we prevented two from doing so. So how's that working? Well, it's going to use ray casting someone look at the code, you'll see that there's now a move player method of the game. It gets passed in a Delta time, that's how much time is elapsed in seconds. Since we last called move plan.
First thing we do is clone the position of a player object. And we ramp up the y value by 60. So that positions it roughly around a thigh, we get the direction which is directly For words of the character, if we've orientated our character correctly, we can use the method of our object 3d, which is get world direction. And in this instance, you'll be the transform Zed axis, and then we use a ray caster. Ray caster allows us to projector Ray forwards from a particular position in a particular direction. So the position is going to be directly from the character and the direction initially is down.
Her is that access, which will rotate round as she rotates around. We then define blocked as being false. So by default, you will be able to move alone through our environment. And we'll look at the environment proxy in a minute. Through our environment children, which effectively is just all the yellow blocks, we call the ray caster intersect object method pass in this particular object. So each one of the yellow boxes in turn, if there is an intersect, then the length of our intersect array is going to be greater than zero, there might be multiple intersects, we're interested in the closest one to us.
These intersects are ordered by distance. So the first one is going to be the closest if the first one is less than 50 units. And that's going to be described as blocked. And we no need to look any further because our character is already blocked. So we can break out of this loop. So only if it's not blocked, and the move forwards value is greater than zero.
That's, that's defined by our joystick Then we actually translate our object along its Zed axis, we also need to know whether or not it's too close to the sides as well. So we cast in the left direction, doing exactly the same thing, but this time, we've got minus one in the x. So it's going along that way. And then we apply the matrix of our objects, so that that particular vector is rotated into world coordinates. And then we create a new Ray caster, do exactly the same thing. It is within the parameters, then we translate the objects to be further out.
If we walk along. A slight angle that will just position is that we moved, slightly skewing along the obstruction We do the same thing in the right direction, we just have a quick look at the creating our dummy environment. First of all, we create a group to put all the blocks in. We give it a name, and we add it to our scene, we create a box geometry, which is 150 units. And then we give it a basic material, which is a yellow color. And then we position many, many of these blocks starting at minus 1000 in the X and incremented in values of 300.
So the 150 wide, a gap of 150 and then another block will appear. We do that in the X and Zed directions. And then we assign environment proxy is a property of our game. And it's the entire group. So That particular group will have lots of children. That's something that we can then use in our ray casting, a diagram might be useful, and it shows how the ray goes forwards.
That's the limit of 50. Once we move forwards, and we get to the point where the length of this ray is less than 50, that is going to be intersecting with this object. And so we don't want to allow it to move forwards anymore. When we cast into the left. That's the effect is going to happen. Casting over in this direction.
And we make our limit slightly wider, shows the method of ray casting. stopping us walking through the blocks