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
  1. /*/
  2. * Script by Devin Curry
  3. * www.Devination.com
  4. * www.youtube.com/user/curryboy001
  5. * Please like and subscribe if you found my tutorials helpful :D
  6. * Twitter: https://twitter.com/Devination3D
  7. /*/
  8. using UnityEngine;
  9. using System.Collections;
  10.  
  11. public class PlayerMovement : MonoBehaviour
  12. {
  13. public float speed = 500f, jumpHeight = 500f;
  14. public LayerMask playerLayerMask;//In the inspector, select everything EXCEPT the "Player" layer
  15. Transform myTrans;
  16. Rigidbody2D myBody;
  17. Vector2 movement;//used as temp holder variable before applying to velocity
  18. bool isGrounded = true;
  19.  
  20.  
  21. void Start ()
  22. {
  23. myTrans = this.transform;
  24. myBody = this.rigidbody2D;
  25. }
  26.  
  27. void Update ()
  28. {
  29. //Check if the player is grounded or not
  30. isGrounded = Physics2D.Linecast(myTrans.position,
  31. GameObject.Find(this.name+"/tag_ground").transform.position,
  32. playerLayerMask);
  33.  
  34. //Just for visualization, not needed for gameplay
  35. Debug.DrawLine (myTrans.position,
  36. GameObject.Find(this.name+"/tag_ground").transform.position,
  37. Color.red);
  38. //Works for keyboards and joysticks
  39. #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
  40. Move (Input.GetAxisRaw("Horizontal"));
  41. if (Input.GetButtonDown ("Jump"))
  42. Jump ();
  43. #endif
  44. }
  45. //
  46. //Separate Move and Jump so they can be called externally by TouchButtons
  47. //
  48. public void Move(float horizontal_input)
  49. {
  50. movement = myBody.velocity;
  51.  
  52. if(isGrounded)//we can only move left and right if on the ground
  53. movement.x = horizontal_input * speed * Time.deltaTime;
  54.  
  55. //Apply the movement to the player
  56. myBody.velocity = movement;
  57. }
  58. public void Jump()//we can only jump if on the ground
  59. {
  60. if(isGrounded)
  61. myBody.velocity += jumpHeight * Vector2.up * Time.deltaTime;
  62. }
  63. }
The Move Buttons:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MoveButton : TouchLogicV2
  5. {
  6. [Range(-1,1)]
  7. public int moveDir = 1;//1=right;-1=left
  8. PlayerMovement player;
  9.  
  10. void Start()
  11. {
  12. player = FindObjectOfType<PlayerMovement>();//This will find our player script, as long as there is only 1 GameObject with "PlayerMovement" on it
  13. }
  14.  
  15. public override void OnTouchBegan()
  16. {
  17. //watch this touch for when it ends anywhere so we can slow down the player
  18. touch2Watch = currTouch;
  19. }
  20.  
  21. public override void OnTouchMoved()
  22. {
  23. player.Move (moveDir);
  24. }
  25. public override void OnTouchStayed()
  26. {
  27. player.Move (moveDir);
  28. }
  29. public override void OnTouchEndedAnywhere()
  30. {
  31. //run this check so other touches ending don't cause player to slow down
  32. if(currTouch == touch2Watch)
  33. player.Move(0);//do avoid annoying drift after letting go of button
  34. }
  35. }
The Jump Button:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class JumpButton : TouchLogicV2
  5. {
  6. PlayerMovement player;
  7. void Start()
  8. {
  9. player = FindObjectOfType<PlayerMovement>();
  10. }
  11. public override void OnTouchBegan()
  12. {
  13. player.Jump ();
  14. }
  15. }