In today's lecture, we’re going through a lot of familiar territories, for the game design, we want our moss monster to walk back and forth patrolling his territory. As with every challenge we’ve taken on, we’re going to be building things in steps until we achieve our goal!
So first, since we’re going to have two waypoints, I’m not going to overcomplicate things this time for adding more waypoints, you can refer to the 2.5D platform tutorial for instructions on how to achieve that. Here, I’m just going to add a couple of transforms to the Enemy class.
And as before, we’ll use the enemy duplication technique to create the waypoints:
- Duplicate the enemy and place the newly duplicated item where they’re going to walk to. For point B, just duplicating is enough.
2. Remove everything but the transforms and rename the points to Point_A_MossGiant and Point_B_MossGiant
3. Drag the waypoints into the MossGiant enemy script:
And we’re all set to start moving!
As with the 2.5D Moving platform tutorial, we’re going to be using Vector2.MoveTowards
Let’s define our MossGiants speed. He’s not nimble or spry, so let’s try 1.5f.
Remember that our Update method MUST be there because our Enemy class demands it!
It’s ok, we’ll be using it in a moment. Good thing we were forced to make it!
But let’s get back to Vector2.MoveTowards, since that’s the method we’ll be using:
Great! All we need to make it work is have the current transform, the target transform, and ‘maxDistanceDelta’ which is essentially how many steps to take or speed…
Let’s run it!
Oops..our speed is an int and I tried to pass in a float…that’s ok, we’ll change it to a float later, let’s just change the speed to 1.
And we might as well write the rest of the code since we did this with our 2.5D platform. We’ll create a holder for our waypoint called _currentPosition that we’ll change every time our enemy reaches a waypoint, we’ll reassign it to the other. We’ll also create a _step variable to hold our speed multiplied by deltatime, and we’ll use that in our MoveTowards method.
Now let’s run it!
I did encounter some issues though. I thought about putting the waypoints under the Moss_Giant parent object but that wouldn’t work because the waypoint numbers for whatever reason weren’t recognized. Instead, I had to create a new Empty called Moss_Giant_Holder, and place both the MossGiant enemy and the waypoints under that, and that thankfully worked!
But let’s get back to the Enemy class now and change the speed to a float!
We’ll have to add a few ‘fs’ to our numbers in the Moss_Giant class.
Well..that was easy…Moving right along…The next thing we need to do is have the moss giant stop at each waypoint. We’ve actually done this before too with our 2.5D Elevator challenge! As we’ve done enough scrolling for one article today, we’ll leave that up for tomorrow! See you then!