Saturday, January 10, 2015

Unity 2D Touch Input Platformer Tutorial

See the other parts here:
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;//Unity 4.6-
  myBody = this.GetComponent<Rigidbody2D>();//Unity 5+
  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;
 }
}