Saturday, March 16, 2013

Player Control: Basic Movement and Camera

Over the past couple days  I decided to work on camera controls and player movement. I wanted to get this done so what ever I work on next will be more enjoyable for me to roam around in or look around at(since I like completing visual goals).

I first wrote a simple script in C# that allowed me to move a box with the W,A,S,D keys. I first version of the script just moved the transform using Vector3 calls. What I mean by this is the transform would just move instantly one space in whatever direction I told it to. This was basic movement but it would not work for my final product. Deciding this would not work for me, I went to the internet and found a simple script on the unity reference pages that allowed me to move my cube much like a car. Using forces this allowed for acceleration and smoother movement. Again, this eventually was not I wanted but it was fun getting something to move and learning more about Unity and scripting. I dropped this script completely and decided to start over!

Camera 

When I decided to work on a new movement script I started with my camera. For my game I want to have a third person camera. I like third person and I find most of the games I play are in third person. I made a list of the camera features I wanted and started working on those.

Camera Features:
-Zoom In/Out
-Rotate around player
-Follow behind player while player is moving
-Snaps to player when character decides to move
-No clipping through floor/objects

With this list in mind I started with the Rotate around player goal. There are several great references online that allow me to get a simple script up and running in no time. Of course, I needed to change a few parameters to make sure the camera fit my needs. I think in about an hour I had a camera that would take a transform target and allow me to move around the player with certain restrictions(like not completely under the player). At first, to rotate the player, you had to use the left mouse button but I changed to allow both or one of the mouse buttons using an "or" statement. Next I worked on the Zoom function. I wrote the code so the player can use the mouse wheel to zoom in/out at their will. After a few hours, I had a camera that followed the player and allowed me to look around. It was awesome I was so happy. The next task, no clipping, was a different story and at first I said "screw it."

Jumping, Spinning, Walking, Running, and "Strafing"

So I decided to skip the camera clipping function and move on to player movement. I went online and found a free character model with some basic animations. I wanted to have something visual to look at. To start with character movement I looked at unity references online and decided to use a character collider for moving. There are two functions for them "simple move" and "move." I use "move" for my script since it allows the most options. I also used the Input Manager that Unity has to keep track of all of my controls. I started working on moving forward and back and rotation. That was simple. I just applied the values that are returned from a ButtonDown command and put thoses on the which axis I needed for that movement. I also worked on making sure the player model had to be on "ground" first before these movements could work. For running, I added a speed modifier to allow me to move faster when I toggled a button. Jumping was a little more complicated. I checked to make sure the player was on the ground and then I added a timer so the player could not continually jump. This took me some time to get working and I still am not perfectly happy with it but its not bad right now! I have strafing working but it does not look good with the animations so I will probably work on it more when I get a better model.

Right now I am working on a Season Script so I can make changes to the Game Time script. I want to change how bright the sun can get and how long the days are and maybe add particle effects later or wind. My next post might be about that but I do not really know!

Dan

No comments:

Post a Comment