Tuesday, February 3, 2015

Unity 2D Platformer Tutorial - Part 3 - Art and Animation

This part 3 is a continuation of the videos found on this page: www.devination.com/2015/01/unity-46-touch-input-platformer-tutorial.html

I've separated part 3 from parts 1 and 2 because in this video we modify existing code from the previous videos to allow for animations. That modified code can be found on this page below the videos while the original code pre-animations can still be found on the previous page.

Part 4 - Enemy Movement is now available here: http://www.devination.com/2015/07/unity-2d-platformer-tutorial-part-4.html


Check out the code below the videos




In this video, part 3 of our 4.6 Platformer series, we add animations to our player as he jumps, walks, and idles. We also add art for the ground tiles and UI buttons.

Part 1: https://www.youtube.com/watch?v=24YR_Uy-MyA
Part 2: https://www.youtube.com/watch?v=gKjKFZ30684

Google+ Community: https://plus.google.com/communities/108584850180626452949
Twitter: https://twitter.com/Devination3D
Facebook: https://www.facebook.com/unity3Dtutorialsbydevin

Time Breakdown:
1:10 - Adding ground tiles
4:10 - Setting up the character sprite sheet
5:55 - Setting up Animations
9:31 - Setting up the Animator
15:48 - Setting up the AnimatorController script
19:00 - FlipArt() function
22:00 - UpdateSpeed() function
24:13 - Updating our PlayerController script
26:45 - Demonstrating our working animations in game





Support Further Tutorial Development
Comments?
Check out the videos above to hear the explanation and see the code in action
Download the project up to this point here: https://drive.google.com/open?id=0Bwiz0Tss2NSqSUZMZkptMFVyM0k
/*/
* Script by Devin Curry
* www.Devination.com
* www.youtube.com/user/curryboy001
* Please like and subscribe if you found my tutorials helpful :D
* Google+ Community: https://plus.google.com/communities/108584850180626452949
* Twitter: https://twitter.com/Devination3D
* Facebook Page: https://www.facebook.com/unity3Dtutorialsbydevin
/*/
using UnityEngine;
using System.Collections;

public class AnimatorController : MonoBehaviour
{
 public static AnimatorController instance;

 Transform myTrans;
 Animator myAnim;
 Vector3 artScaleCache;

 void Start ()
 {
  myTrans = this.transform;
  myAnim = this.gameObject.GetComponent<Animator> ();
  instance = this;

  artScaleCache = myTrans.localScale;
 }

 void FlipArt(float currentSpeed)
 {
  if((currentSpeed < 0 && artScaleCache.x > 0)|| //going left AND faceing right OR...
   (currentSpeed > 0 && artScaleCache.x < 0)) //going right AND facing left
  {
   //flip the art
   artScaleCache.x *= -1;
   myTrans.localScale = artScaleCache;
  }
  
 }

 public void UpdateSpeed(float currentSpeed)
 {
  myAnim.SetFloat ("Speed", currentSpeed);
  FlipArt(currentSpeed);
 }

 public void UpdateIsGrounded(bool isGrounded)
 {
  myAnim.SetBool ("isGrounded", isGrounded);
 }
}

And this is the modified PlayerController script
/*/
* Script by Devin Curry
* www.Devination.com
* www.youtube.com/user/curryboy001
* Please like and subscribe if you found my tutorials helpful :D
* Google+ Community: https://plus.google.com/communities/108584850180626452949
* Twitter: https://twitter.com/Devination3D
* Facebook Page: https://www.facebook.com/unity3Dtutorialsbydevin
/*/
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
 public float speed = 10, jumpVelocity = 10;
 public LayerMask playerMask;
 public bool canMoveInAir = true;
 Transform myTrans, tagGround;
 Rigidbody2D myBody;
 bool isGrounded = false;
 float hInput;
 AnimatorController myAnim;
 
 void Start ()
 {
//  myBody = this.rigidbody2D;//Unity 4.6-
  myBody = this.GetComponent<Rigidbody2D> ();//Unity 5+
  myTrans = this.transform;
  tagGround = GameObject.Find (this.name + "/tag_ground").transform;
  myAnim = AnimatorController.instance;
 }
 void FixedUpdate ()
 {
  isGrounded = Physics2D.Linecast (myTrans.position, tagGround.position, playerMask);
  myAnim.UpdateIsGrounded (isGrounded);
  
  #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
  hInput = Input.GetAxisRaw("Horizontal");
  myAnim.UpdateSpeed(hInput);
  if(Input.GetButtonDown("Jump"))
   Jump();
  #endif

  Move(hInput);
 }
 
 void Move(float horizonalInput)
 {
  if(!canMoveInAir && !isGrounded)
   return;
  
  Vector2 moveVel = myBody.velocity;
  moveVel.x = horizonalInput * speed;
  myBody.velocity = moveVel;
 }
 
 public void Jump()
 {
  if(isGrounded)
   myBody.velocity += jumpVelocity * Vector2.up;
 }
 
 public void StartMoving(float horizonalInput)
 {
  hInput = horizonalInput;
  myAnim.UpdateSpeed(horizonalInput);
 }
}