Singapore-MIT GAMBIT Game Lab spacer
  CMS MIT
spacer New Entries Archives Links subheader placeholder
Updates
left edge
pinstripe
More on Unity3d

Still wading through the Unity documentation. Today's project: incorporating keyboard input. Finding the relevant pages in the Unity documentation was fairly easy; I searched for "key" and found KeyCode, which led me to the Event class. However, it took a while for me to figure out how to use the Event class because the relevant pages did not provide any sample code. I tried using Event objects in the Update function and kept getting errors. Eventually I figured out that whenever an event occurs, the script runs the OnGUI function, so I had to access the event from there. Example:


// If a keyboard event occurs, print the key pressed
void OnGUI(){
        if (Event.current.isKey){
                KeyCode k = Event.current.keyCode;
                print(k);
         }
}

Once I had keyboard input up and running, I wrote a simple program to move the avatar using the arrow keys. So far, so good. The next step was restricting the avatar's movement. For the Abandon game, the player needs to stay within the bounds of the maze, so I set up a few test walls:

walls.JPG

Unity has a lot of physics functions built in. Most objects automatically have a Collider component, so all I had to do was add a Rigidbody component to the avatar to make it unable to walk through walls. Unfortunately, in this case the physics functions were a little too accurate for my purposes; when the avatar hit a wall, it bounced off the wall and fell over. Next step: figuring out how to make it stay upright.

right edge
bottom curves