Monday, 3 March 2014

Animations and first coding

3. Player animations and movement

To apply simple animations e.g. for character in move I used tile sheets which contain player sprites captured frame by frame. At this stage I used three types of animations: Idle, Walk and Shoot but I'm going to include one more for Player hit. Each animation needs a condition which will trigger it. In Unity each PNG file containing tile sheet for animation needs to be prepared. I set: Sprite Mode - Multiple, Pixels To Units - 32 then in Sprite Editor I used grid to divide picture into frames, each frame needs to have the same size in pixels. Next I created new object for player called Animator, each animator needs Controller. In Animation Window for player I added: Idle with Sample Rate - 3, Walk with Sample Rate - 12 and Shoot with Sample Rate - 20. Sample Rates are set how fast animation will be displayed. In Animator Window I set transition between animations and conditions as follow: if speed of player is greater than 0.01 switch to Walk animation, if speed of player is less than 0.01 go back to default Idle animation. Reason why I used values 0.01 is if we use e.g. 1 switching animations will be delayed as player has to reach speed of 1 first. For shooting I created new variable called 'Trigger' and I set it to true in Update function in PlayerControl scrip using Input.GetMouseButtonDown(0). Animation will end after 1 second - Exit Time variable.
For movement I created new C# script called PlayerControl. I included two variables: maxSpeed for player speed set it to 3.0f and Animator type variable called anim to be able to access animations. In update function I included another variables for speedX and speedY assigning them values: "Input.GetAxis("Horizontal")" and "Input.GetAxis("Vertical")". I also created new object called rigidbody2D for my player as i I will need it to change velocity of it "Vector2(moveX * maxSpeed, moveY * maxSpeed)", detect any collisions or to apply any physics.

Example of "Animator" tab for my player:


Picture above clearly represents bunch of different animations. We have two different sets of animations, animations for player holding pistol and for player holding shoot gun.

No comments:

Post a Comment