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

Wednesday, December 10, 2014

Unity 4.6 UI Touch and Mouse Input Tutorial


This Unity touch tutorial for Unity 4.6+ will show you how to call custom functions on any object in your scene when you touch or click on UI elements. This is perfect for menus, on-screen touch controls, and anything else that you want to happen when you touch/click on UI buttons.

Time Breakdown:
0:45 - I'm planning on redoing some of my old TouchLogic videos to work with the 4.6 UI
1:04 - How to create a GUITexture in Unity 4.6 (if you still need to use that old system)
1:45 - Creating a 4.6 GUI Button, explaining what the various gameobjects do
2:29 - Click / touching the button with the Unity remote... multitouch is limited on the Unity Remote
3:18 - Multitouch does work if you build the game and play it on your mobile device
3:50 - OnClick and the Event System
4:30 - Creating a custom script to call OnClick
5:53 - Image class is found in the UnityEngine.UI namespace
7:40 - Demonstration of calling our custom function OnClick
8:20 - Adding an EventTrigger component for more event types
9:55 - Demonstration of multitouch on an Android build
10:52 - Outro, thanks for watching :D