- Home /
Failing every method for touch point in Android screen
I can't see why on earth this script runs perfectly OK in the Unity Editor but fails in Android tablet. I suspect that the problem is in getting the mouse events because it is not going inside the if
block no matter the method I use as condition (see the +++comment+++ in code), probably due to the difference between mouse and finger positioning. Yet I can't figure out the solution. I coded a sound to be played in the if block, so that I know where exactly the problem is.
This is a 2D game where I must draw a line over a predefined path (collider 'area_0' or 'area_1') without getting out of it. So I'm raycasting finger position against the collider.
In Unity Editor works like a charm, once I push it to the tablet it just doesn't.
I adapted the line drawing script some guy published in YouTube tutorial.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dibujar : MonoBehaviour {
private Color color;
void Start() {
color = new Color32(0xFF, 0xED, 0x00, 0xFF);
}
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(0)) {
//if (Input.GetKeyDown(KeyCode.Mouse0))
// +++++++ DOES NOT GET IN HERE +++++++
StartCoroutine(dibujar());
}
}
IEnumerator dibujar() {
LineRenderer linea = new GameObject().AddComponent<LineRenderer>();
List<Vector3> posiciones = new List<Vector3>();
bool borrar = false;
linea.startWidth = 0.2f;
linea.endWidth = 0.2f;
linea.material = new Material(Shader.Find("Unlit/Color"));
linea.material.color = color;
linea.transform.SetParent(transform);
linea.numCapVertices = 1;
linea.numCornerVertices = 1;
while (Input.GetMouseButton(0)) {
//while (Input.GetKey(KeyCode.Mouse0)) {
//while (Input.touchCount > 0 || Input.touches[0].phase == TouchPhase.Began) {
posiciones.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward);
linea.numPositions = posiciones.Count;
linea.SetPositions(posiciones.ToArray());
Vecto3 rayo = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.DrawRay(rayo, Vector3.forward * 20);
RaycastHit2D hit = Physics2D.Raycast(rayo, Vector3.forward, Mathf.Infinity);
if (hit.collider == null) {
// if no hit
}
borrar = true;
break; // force while loop to stop drawing
}
if (hit.collider.gameObject.name == "area_0") {
// if hit area_0
}
if (hit.collider.gameObject.name == "area_1") {
// if hit area_1
}
yield return new WaitForSeconds(0);
}
if (borrar) {
// erase line
}
}
}
BTW, I have changed Vector3.forward
for Vector2.zero
with no different result. The problem doesn't seem to be in raycasting. Or does it?
$$anonymous$$aybe it gets confused because of coroutine?