Tuesday, January 21, 2014

Unity TouchLogic3D Tutorial - Improved Touch Input


Check out the code below the videos



In this Unity tutorial we will separate and improve the 3D portion of our TouchLogic script (original here) making it more optimized for better performance. I will rewrite and better explain a few things to solve any issues you may have had with the original.

Time Breakdown:
0:10 - Overview of how and what we will be doing
1:00 - How to make this script work for 2D colliders
1:28 - Why are we separating TouchLogic3D and TouchLogicV2?
2:25 - Creating the ITouchable3D interface
4:45 - Implementing our interface in our script for touchable objects
8:00 - Creating our TouchLogic3D script
10:00 - Checking what the raycast hit
12:28 - Checking if what we hit is actually touchable
15:12 - Demonstrating that our script recognizes what is touchable and what is not
16:23 - Removing SendMessage and calling functions directly
19:24 - Finished with the touch functions, moving on to the Anywhere functions
23:15 - Demonstrating why we need a touch2Watch variable
25:30 - Outro, thanks for watching :D



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 TouchLogic3D : MonoBehaviour 
{
 public static int currTouch = 0;//so other scripts can know what touch is currently on screen
 private int touch2Watch = 64;
 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
 private ITouchable3D touchedObject = null;
 
 void Update () 
 {
  foreach(Touch touch in Input.touches)
  {
   currTouch = touch.fingerID;
   ray = Camera.main.ScreenPointToRay(touch.position);//creates ray from screen point position
   //if the ray hit something, only if it hit something
   if(Physics.Raycast(ray, out rayHitInfo))
   {
    //if the thing that was hit implements ITouchable3D
    touchedObject = rayHitInfo.transform.GetComponent(typeof(ITouchable3D)) as ITouchable3D;
    //Debug.Log (touchedObject);
    //If the ray hit something and it implements ITouchable3D
    if (touchedObject != null)
    {
     switch(touch.phase)
     {
     case TouchPhase.Began:
      touchedObject.OnTouchBegan();
      touch2Watch = currTouch;
      break;
     case TouchPhase.Ended:
      touchedObject.OnTouchEnded();
      break;
     case TouchPhase.Moved:
      touchedObject.OnTouchMoved();
      break;
     case TouchPhase.Stationary:
      touchedObject.OnTouchStayed();
      break;
     }
    }
   }
   //On Anywhere
   else if (touchedObject != null && touch2Watch == currTouch)
   {
    switch(touch.phase)
    {
    case TouchPhase.Ended:
     touchedObject.OnTouchEndedAnywhere();
     touchedObject = null;
     break;
    case TouchPhase.Moved:
     touchedObject.OnTouchMovedAnywhere();
     break;
    case TouchPhase.Stationary:
     touchedObject.OnTouchStayedAnywhere();
     break;
    }
   }
  }
 }
}
And the interface:
/*/
* 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
/*/
public interface ITouchable3D 
{
//Remove functions you know you arent going to use
// void OnNoTouches();
 void OnTouchBegan();
 void OnTouchEnded();
 void OnTouchMoved();
 void OnTouchStayed();
// void OnTouchBeganAnywhere();
 void OnTouchEndedAnywhere();
 void OnTouchMovedAnywhere();
 void OnTouchStayedAnywhere();
}