Sunday, June 2, 2013

Unity Touch Input Tutorials


Check out the code below the videos

This Unity tutorial (Part 1 of 2) will show you how to make your game recognize Touch Inputs from the player and create on-screen buttons that run functions when touched.

Time Breakdown:
0:59 - Difference between mouse and touch Inputs
4:39 - Cons of using Mouse function for touchscreens
6:20 - Talk about inheriting
7:35 - Starting the TouchButtonLogic script
9:48 - Looping through all the touches on screen
12:00 - Check to see if touch is on our guitexture button
13:42 - Check to see if the touch began or ended
15:46 - Demonstrating our script on the touch screen
16:45 - Outro - Overview of what we'll do in the next video



This Unity tutorial (Part 2 of 2) will show you how to make your game recognize Touch Inputs from the player and create on-screen buttons that run functions when touched.

Time Breakdown:
0:37 - Calling functions after our touch events
1:30 - Difference between calling a function and sending a message
2:50 - Making our new touch button script inherit from our TouchButtonLogic script
4:01 - Showing that our new script works on the touch device
4:22 - Benefits of making our new scripts inherit from our TouchButtonLogic script
4:39 - Showing how easy it is to make new touch button scripts with different functionalities
6:13 - Final demonstration of everything working on the touch device


This Unity tutorial will show you how to make your game recognize Touch Inputs from the player and have them call functions on your 3D GameObjects that are touched. We will be utilizing new functions dealing with Raycasts.

Time Breakdown:
0:28 - OnTouchAnywhere, function calls that happen when you're touching outside of the GUITexture
1:10 - public static variable, a global variable
2:30 - Touch functions on Colliders instead of GUITextures
3:00 - Rays, what they do and why we need them
4:42 - Casting a ray from our touch into the scene
6:10 - Finding the GameObject that was hit by our ray
6:56 - Call a function on that GameObject
7:30 - Demonstration that it works
9:22 - Outro, final thoughts, plans for next video


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 TouchLogic : MonoBehaviour 
{
 public static int currTouch = 0;//so other scripts can know what touch is currently on screen
 private Ray ray;//this will be the ray that we cast from our touch into the scene
 private RaycastHit rayHitInfo = new RaycastHit();//return the info of the object that was hit by the ray
 [HideInInspector]
 public int touch2Watch = 64;
  
 void Update () 
 {
  //is there a touch on screen?
  if(Input.touches.Length <= 0)
  {
   //if no touches then execute this code
  }
  else //if there is a touch
  {
   //loop through all the the touches on screen
   for(int i = 0; i < Input.touchCount; i++)
   {
    currTouch = i;
    Debug.Log(currTouch);
    //executes this code for current touch (i) on screen
    if(this.guiTexture != null && (this.guiTexture.HitTest(Input.GetTouch(i).position)))
    {
     //if current touch hits our guitexture, run this code
     if(Input.GetTouch(i).phase == TouchPhase.Began)
     {
      //need to send message b/c function is not present in this script
      //OnTouchBegan();
      this.SendMessage("OnTouchBegan");
     }
     if(Input.GetTouch(i).phase == TouchPhase.Ended)
     {
      //OnTouchEnded();
      this.SendMessage("OnTouchEnded");
     }
     if(Input.GetTouch(i).phase == TouchPhase.Moved)
     {
      //OnTouchMoved();
      this.SendMessage("OnTouchMoved");
     }
     if(Input.GetTouch(i).phase == TouchPhase.Stationary)
     {
      //OnTouchStayed();
      this.SendMessage("OnTouchStayed");
     }
    }
    
    //outside so it doesn't require the touch to be over the guitexture
    ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);//creates ray from screen point position
    switch(Input.GetTouch(i).phase)
    {
    case TouchPhase.Began:
     //OnTouchBeganAnywhere();
     this.SendMessage("OnTouchBeganAnyWhere");
     if(Physics.Raycast(ray, out rayHitInfo))
      rayHitInfo.transform.gameObject.SendMessage("OnTouchBegan3D");
     break;
    case TouchPhase.Ended:
     //OnTouchEndedAnywhere();
     this.SendMessage("OnTouchEndedAnywhere");
     if(Physics.Raycast(ray, out rayHitInfo))
      rayHitInfo.transform.gameObject.SendMessage("OnTouchEnded3D");
     break;
    case TouchPhase.Moved:
     //OnTouchMovedAnywhere();
     this.SendMessage("OnTouchMovedAnywhere");
     if(Physics.Raycast(ray, out rayHitInfo))
      rayHitInfo.transform.gameObject.SendMessage("OnTouchMoved3D");
     break;
    case TouchPhase.Stationary:
     //OnTouchStayedAnywhere();
     this.SendMessage("OnTouchStayedAnywhere");
     if(Physics.Raycast(ray, out rayHitInfo))
      rayHitInfo.transform.gameObject.SendMessage("OnTouchStayed3D");
     break;
    }
   }
  }
 }
}