Second Wind: It’s Alive!

Last week I set myself the goals of completing the character controller and beginning basic enemy AI.  But due to a busy schedule and some hiccups with the code, I was forced to concede with an incomplete character controller.  This week should see my character controller finished and have most of my enemy AI figured out.

So after scouring the API references for a little bit I found what may be the answer to all my solutions.  Two functions, the first being “Screen.lockCursor” which supposedly will lock the cursor to within the screen as the app is running, and a second function “Mathf.Clamp” which will prevent movement or rotation past set positions.

Immediately I had a win with the “lockCursor” function, after implementing it into the ‘Start’ function the cursor did exactly what I wanted it to do and locked into the apps screen upon start.  Clamping the camera rotation to the x-axis however did not have the effect I was hoping.  After numerous attempts I was forced to concede avoiding a repeat of the previous weeks delays.  This is the code I ended up with;

float y = Input.GetAxis(“Mouse Y”);
   Vector3 newRotation = _myTransform.localEulerAngles;
   newRotation.x += -y * _mouseSensitivity;
   newRotation.x = Mathf.Clamp(newRotation.x, -290, 70); 

Even though I wasn’t able to figure out the ‘Clamp’, I was happy with the finished result of the character controller and it was performing as I hoped it would.  With that out of the way, I needed to get the majority of my enemy AI done now.  For this assessment I need to do three unique enemy AI’s to receive full marks.  Immediately I wanted a shooting enemy, a melee enemy, and an enemy that performs a jump attack or a special attack within a certain range from the player.

Having mastered the code needed to work out distances and directions, I managed to get the enemies all aggressing the player once they were within a certain radius.  There was something empowering about achieving this basic level of AI.  Going from a static model or cube, then giving it behaviours that react when certain conditions are met is like they’re coming alive.

My zombie enemies are now working as intended, I wish I knew how to animate them (one day!).  My soldier enemies, are now shooting at the player, however, I need to fine-tune their colliders as they sometimes clip on their own projectiles.  My spiders though, I cant figure out the logic to get them to jump at the player.  For now I have given them the same AI as the zombies and I hope I will have time at the end of the project to readdress the issue.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s