Help with Multitouch input on Android - Unity

Hi Guys,

I'm busy trying to port my comp E game to play on Android Touch devices. It's all new to me and I am a bit lost and need help. My game was set up in such a way that the character movement was handled with AWSD and my shooting with the ARROW keys (kinda like a twin stick shooter). All my animations and movement used Input.GetkeyDown and Input.GetKeyUp type logic. I am using the new GUI Canvas Images as my buttons. Each of these buttons has 2 scripts attached to it. My button logic, which is a hacky way of getting getkeydown and getkeyup behavior and then a second script (unique for each button) to say what the button needs to do when the button is up or when the button is down. Basically change the values of static public bools in my character movement script. Here is an example of the code attached to my move up button:

Button Logic:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class TouchButtonsLogic : MonoBehaviour, IPointerDownHandler, IPointerUpHandler 
{
	
	private int pointerID;

	public void OnPointerDown (PointerEventData data) 
	{
		pointerID = data.pointerId;
		this.SendMessage("TouchyDown");
	}
	
	public void OnPointerUp (PointerEventData data) 
	{
		if (data.pointerId == pointerID) 
		{
			this.SendMessage("TouchyUp");
		}
	}
	
}


Up Button Script:

using UnityEngine;
using System.Collections;

public class UpTouchButton : MonoBehaviour
{

	void TouchyDown()
	{
		CharacterMovement.SPressed = false;
		CharacterMovement.Slifted = false;
		CharacterMovement.WPressed = true;
		CharacterMovement.Wlifted = false;
		CharacterMovement.APressed = false;
		CharacterMovement.Alifted = false;
		CharacterMovement.DPressed = false;
		CharacterMovement.Dlifted = false;
		CharacterMovement.WDPressed = false;
		CharacterMovement.WDlifted = false;
		CharacterMovement.WAPressed = false;
		CharacterMovement.WAlifted = false;
		CharacterMovement.SDPressed = false;
		CharacterMovement.SDlifted = false;
		CharacterMovement.SAPressed = false;
		CharacterMovement.SAlifted = false;
	}

	void TouchyUp()
	{
		CharacterMovement.SPressed = false;
		CharacterMovement.Slifted = false;
		CharacterMovement.WPressed = false;
		CharacterMovement.Wlifted = true;
		CharacterMovement.APressed = false;
		CharacterMovement.Alifted = false;
		CharacterMovement.DPressed = false;
		CharacterMovement.Dlifted = false;
		CharacterMovement.WDPressed = false;
		CharacterMovement.WDlifted = false;
		CharacterMovement.WAPressed = false;
		CharacterMovement.WAlifted = false;
		CharacterMovement.SDPressed = false;
		CharacterMovement.SDlifted = false;
		CharacterMovement.SAPressed = false;
		CharacterMovement.SAlifted = false;
	}
}



Now this all works fine, and each button does what it is supposed to do. The problem is that is not allowing for multiple touches. Example: I can move up (with up button), and I can shoot up (with shoot up button), but I cannot move up and shoot up at the same time (press both up button and shoot up button). I tried google but couldn't find anything that made sense. A lot of the advice out there is still for GUItextures in stead of Canvas Images. Can anyone assist me please!!!! As I said this is my first attempt at anything Touch and I have no idea what I'm doing.

Thanks

Comments

  • edited
    So, I would use the Input class instead of the new gui stuff because you can get a lot more useful information out of it
    void Update()
    {
        // Make sure there are at least two touches on the screen
        if (Input.touchCount > 1)
        {
    	// Positional information, in screen space
    	Debug.Log(Input.GetTouch(0).position); // First touch location
    	Debug.Log(Input.GetTouch(1).position); // Second touch location 
    	Debug.Log(Input.GetTouch(0).deltaPosition); // Change in position in screen space
    			
    	// Touch states
    	Debug.Log(Input.GetTouch(0).phase); // Possible phases are began, canceled, ended, moved, stationary
    
            // Example usage
    	if (Input.GetTouch(0).phase == TouchPhase.Began)
    	{
    	    Debug.Log("FINGER DOWN");
    	}
    
            // This will throw an error, which is why we need to check how many touches there are
            Debug.log(Input.GetTouch(2).position); // Assuming there are two fingers on the screen only	
        }    
    }
    Thanked by 2dislekcia FanieG
  • edited
    Also, assuming you want twin stick shooter controls (and you're using the latest version of Unity), Unity now has those in the standard assets!
    Assets --> Import Package --> Cross Platform Input. Look for a prefab called DualTouchControls in the prefabs folder.

    EDIT: Also available for Unity 4.6
  • @Stray_Train thank you so much for the detailed response. The input class does really look cool and I learned a whole lot over the weekend from the script you provided - So thank you again. Also, I knew about the prefab in the standard assets, however I wanted to try and write it myself to see how things work under the bonnet so to speak. Also, as the game I was porting was already finished on PC. I did not want to go about changing all my logic in order to allow for the new touch controls, and that is why I attempted to hack something out that still used the same logic. As it turned out the problem with my multi-touches was due to the Unity remote, that does not allow multiple touches (standalone input module component was the culprit). I made an Android build and the multi-touches worked just fine as I had it, once deployed on the device. I did change the Canvas images I was using to Canvas Buttons and then used Event Triggers to handle my Pointer Up and Pointer Downs in a similar way as what I had my on buttonDown and onbuttonups that I initially had. Also learned a whole lot about texture compression, so all and all a very productive weekend.

    I do have another question though:

    If I "Build and Run" the game gets sent to my device. But how would I go about getting the game onto other devices without connecting them to my PC, or posting an Android build here for instance? If I just build, Unity gives me a single .apk file, what do I do with this? Sorry for the noob questions, but as stated earlier, I am completely new to this.
  • edited
    apk (android application package) is the android version of windows/mac install files. Think of it as a container that holds all of your game files in one convenient package. So, you distribute apk files like you would distribute exe files on windows. I load them onto my android device via usb (I could even download them if you host it on dropbox or something similar) and install it using a file browser. By default, you can't install applications that aren't from the play store on android devices. You should instruct your playtesters to allow apps to be installed from external sources. Although here on the forums it's safe to assume that most people have already done this.
  • If you (or anyone else reading this) would like, I could write up a tutorial on how to deploy to android from unity and what all the android specific terms mean
  • If you (or anyone else reading this) would like, I could write up a tutorial on how to deploy to android from unity and what all the android specific terms mean
    Dude, I cannot even begin to tell you how grateful I would be if you did this!!!!
Sign In or Register to comment.