Sunday, June 22, 2014

Unity Touch Button Platformer - Move the player with touch controls



Check out the code below the videos




Many of you have asked for the project files for this. I've made the project available in a unity package here: https://5162f57a653eade62cbf89f301530be765718446.googledrive.com/host/0B-lqJgA6nVdXTE9Ia2syc0NEMjg/

This Unity tutorial will show you how to make a Player move and jump with on-screen touch buttons for a mobile game.

Twitter: https://twitter.com/Devination3D

To connect your device to unity, download the unity remote app and follow these steps: http://answers.unity3d.com/questions/198853/unity-remote-for-android-not-working-solution.html

Time Breakdown:
0:28 - Setting up the player GameObject
2:18 - Starting the PlayerMovement script
4:25 - Move Function
6:42 - Setting up keyboard controls for Movement
7:40 - Explaination of InputManager (skip this section if you're already familiar with it)
8:57 - Jump Function
8:45 - Setting up keyboard controls for Jump
11:00 - GetButton vs GetButtonDown
11:45 - Defining up platform dependant code blocks with #if and #endif
14:25 - Checking if player isGrounded with LineCasting
19:20 - Demonstration of LineCasting
20:08 - Setting up Touch Buttons
21:31 - Setting up the Move Touch Button
26:44 - Setting up the Jump Touch Button
28:05 - Demonstration on Touch Screen Android

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
* Twitter: https://twitter.com/Devination3D
/*/
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour 
{
 public float speed = 500f, jumpHeight = 500f;
 public LayerMask playerLayerMask;//In the inspector, select everything EXCEPT the "Player" layer
 Transform myTrans;
 Rigidbody2D myBody;
 Vector2 movement;//used as temp holder variable before applying to velocity
 bool isGrounded = true;


 void Start () 
 {
  myTrans = this.transform;
  myBody = this.rigidbody2D;
 }

 void Update () 
 {
  //Check if the player is grounded or not
  isGrounded = Physics2D.Linecast(myTrans.position,
                                  GameObject.Find(this.name+"/tag_ground").transform.position,
                                  playerLayerMask);

  //Just for visualization, not needed for gameplay
  Debug.DrawLine (myTrans.position,
                  GameObject.Find(this.name+"/tag_ground").transform.position,
                  Color.red);
  
  
  //Works for keyboards and joysticks
  #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
  Move (Input.GetAxisRaw("Horizontal"));
  if (Input.GetButtonDown ("Jump"))
   Jump ();
  #endif
 }
 //
 //Separate Move and Jump so they can be called externally by TouchButtons
 //
 public void Move(float horizontal_input)
 {
  movement = myBody.velocity;

  if(isGrounded)//we can only move left and right if on the ground
   movement.x = horizontal_input * speed * Time.deltaTime;

  //Apply the movement to the player
  myBody.velocity = movement;
 }
 public void Jump()//we can only jump if on the ground
 {
  if(isGrounded)
   myBody.velocity += jumpHeight * Vector2.up * Time.deltaTime;
 }
}
The Move Buttons:
using UnityEngine;
using System.Collections;

public class MoveButton : TouchLogicV2 
{
 [Range(-1,1)]
 public int moveDir = 1;//1=right;-1=left
 PlayerMovement player;

 void Start()
 {
  player = FindObjectOfType<PlayerMovement>();//This will find our player script, as long as there is only 1 GameObject with "PlayerMovement" on it
 }

 public override void OnTouchBegan()
 {
  //watch this touch for when it ends anywhere so we can slow down the player
  touch2Watch = currTouch;
 }

 public override void OnTouchMoved()
 {
  player.Move (moveDir);
 }
 public override void OnTouchStayed()
 {
  player.Move (moveDir);
 }
 public override void OnTouchEndedAnywhere()
 {
  //run this check so other touches ending don't cause player to slow down
  if(currTouch == touch2Watch)
   player.Move(0);//do avoid annoying drift after letting go of button
 }
}
The Jump Button:
using UnityEngine;
using System.Collections;

public class JumpButton : TouchLogicV2 
{
 PlayerMovement player;
 void Start()
 {
  player = FindObjectOfType<PlayerMovement>();
 }
 
 public override void OnTouchBegan()
 {
  player.Jump ();
 }
}