- Home /
Touch: works on Unity-Remote, doesn't work on Android device after building
In my Android game I want to use the perspective camera. When the user touches something with a specified name something happens. Using Unity-Remote on my android device everything works well, but when I build the game, the script on my android device doesn't work (probabily I think the ray isn't well cast.. but I have no idea why!) Could you kindly help? This is my script:
private var levelStateMachine : LevelStatus; // link to script that controls the level.
var cam: Camera;
function Start () {
levelStateMachine = FindObjectOfType(LevelStatus);
if (!levelStateMachine)
Debug.Log("No link to Level Status");
}
function Update () {
if(levelStateMachine.touchEnabled){
// Rileva il tocco
if((Input.touchCount>0) && (Input.GetTouch(0).phase == TouchPhase.Ended)) {
var gameObject : GameObject = GetClickedItem(Input.GetTouch(0).position);
Debug.Log(gameObject.name);
if(gameObject.name == "Heart") levelStateMachine.heartBeat(1);//levelStateMachine.heartBeat(1); //contatore -1;
else if(gameObject.name == "FHeart" || gameObject.name == "SHeart") levelStateMachine.heartBeat(3); //contatore -3;
else if(gameObject.name == "+Heart") levelStateMachine.heartBeat(2); //contatore -2
}
}
if(Input.GetKey("up") && levelStateMachine.touchEnabled) {
levelStateMachine.heartBeat(1);
}
}
// Rileva l'elemento cliccato
function GetClickedItem(pos: Vector2) {
var camera : Camera = cam; //Camera.main;
var vektor = Vector3(pos[0],pos[1],0f);
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (vektor);
if (Physics.Raycast(ray, hit, Mathf.Infinity)) return hit.collider.gameObject;
else return null;
}
//DEBUG
function OnMouseDown(){
if(levelStateMachine.touchEnabled){
if((this.name == "Heart")) levelStateMachine.heartBeat(2);
}
}
Unfortunately, using Unity remote isn't the same as a build. It's like having the editor run on your phone.
An analogy that kinda fits,
You can play a video on a tv straight through a pc with hdmi, but this doesnt tell you if the file itself would run directly played on the tv.
Thanks, now I see... What can I do in order to make the script work? Do you have any suggestions about the code?
Answer by meat5000 · Oct 22, 2013 at 07:27 PM
if((Input.touchCount>0) && (Input.GetTouch(0).phase == TouchPhase.Ended))
Id say the above code is the problem.
Acquire the information you want in phase began. You can process it in Ended.
Also, use a different name than gameObject, as this particular name refers to the gameObject the script is attached to. On top of that, you will need to declare this object Outside of Update loop so it has scope class-wide.
Answer by just_jim · Feb 10, 2014 at 11:28 AM
I just overcome the same problem. The problem was, that I had 2 spots in my code that I did input check. One was in the Update() where I was checking for TouchInputs and I was calling a foo() methods if I detected a touch, and also I had a onMouseDown() methode (because I was developing in a computer ofcource and I wanted to test the game with mouse clicks) that called the same foo() methode. Propably (it is very comon) in a game, a touch does a selection. You tap something to select it, and propably you hav coded already that if you select it again it deselects. So if you have both methodes (touch and mouse) because unity on a mobile translates touches to mouse events, you'll call foo() twice. So your object will be selected and deselected on an instance and you would think that touch didn't work. But it did. So check if you have a leftOver OnMouseDown methode somewhere and comment it out before you build. I hope taht helped...
Your answer
Follow this Question
Related Questions
Export & Install APK on Android 5 Answers
Sensitivity on Android Input.acceleration 2 Answers
Why do my GUI elements move when exported to Android? 1 Answer
Bugsense in unity(android) 1 Answer
Android uses Bluetooth keyboard as input 0 Answers