Tuesday, February 3, 2015

Unity 4.6 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.


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
14:48 - Setting up the 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();
  instance = this;

  artScaleCache = myTrans.localScale;
 }

 void FlipArt(float currentSpeed)
 {
  if((currentSpeed < 0 && artScaleCache.x == 1)|| //going left AND faceing right OR...
   (currentSpeed > 0 && artScaleCache.x == -1)) //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;
  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);
 }
}

Saturday, January 10, 2015

Unity 4.6 Touch Input Platformer Tutorial



Check out the code below the videos




In part 1 of this video we will learn how to move a player left, right, and jump using keyboard and joysticks. In part 2 we add on-screen touch input buttons for mobile and move our player with those.

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

Time Breakdown:
0:55 - Setting up the test level scene
2:17 - Setting up the player physics2D object
4:15 - Setting up the player controller script
6:22 - Creating the Move function
7:50 - Explanation of the Input Manager
10:04 - Demonstration of player movement
10:36 - Creating the Jump function
12:39 - Demonstration of Jump function
13:21 - Adding the isGrounded check
16:57 - Demonstration of isGrounded and why we need a LayerMask
17:28 - Adding the LayerMask
18:18 - Setting up the Player layer for our mask
19:05 - Outro, what we will cover in Part 2



In this video, part 2 of our 4.6 Platformer series, we add on-screen touch input buttons for mobile and move our player with those but calling the functions we wrote in part 1. We also add slick edges to our platforms so we don't stick to them if we move towards them while in the air.

Time Breakdown:
0:50 - Explaining different possible jump behaviours
1:38 - Adding the option to disable new movement while in air
2:50 - Demonstration of the new jump behaviour
3:20 - Adding a slick PhysicsMaterial2D to the platform
5:05 - Only adding the slick material to the edges of the platform
7:07 - Creating touch buttons
8:09 - #Ifdef disable keyboard controls for mobile
9:32 - Adding Event trigger events to our touch button
10:55 - Explaination of new move function we need
11:24 - Writing the StartMoving function
13:47 - Adding the Jump button
15:37 - Destroying the touch buttons if not on mobile
17:44 - Setting up build settings to run on Android
19:06 - Scale touch buttons using Canvas Scaler
19:51 - Demonstration of touch buttons on Android build
20:26 - What will we do in part 3? Animate the character



Support Further Tutorial Development
Comments?
Check out the videos above to hear the explanation and see the code in action
/*/
* 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 = 0;

 void Start ()
 {
  myBody = this.rigidbody2D;
  myTrans = this.transform;
  tagGround = GameObject.Find (this.name + "/tag_ground").transform;
 }
 
 void FixedUpdate ()
 {
  isGrounded = Physics2D.Linecast (myTrans.position, tagGround.position, playerMask);

  #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
  Move(Input.GetAxisRaw("Horizontal"));
  if(Input.GetButtonDown("Jump"))
     Jump();
  #else
  Move (hInput);
  #endif
 }

 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;
 }
}

Wednesday, December 24, 2014

Unity 4.6 Canvas Scaler



This Unity tutorial will show you how to make your Unity 4.6+ UI scale to match any resolution and aspect ratio with the Canvas Scaler component. See here for how to do it with Anchors: https://www.youtube.com/watch?v=svyDgYz5idg
And here for how to do this for the old GUIText: https://www.youtube.com/watch?v=_9mOh7_xX2o

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