- Home /
Raycasting freezes the Android device?
Hello, guys, I was having issues with this bit of code:
var ray;
var hit : RaycastHit;
var obj : infoLibro;
var vektor : Vector3;
if(Input.touches.Length > 0) {
vektor = Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0f);
ray = Camera.main.ScreenPointToRay (vektor);
if(Physics.Raycast(ray, hit, 100)) {
if(hit.transform.gameObject.tag == "libro") {
nameObj = hit.collider.gameObject.name;
hRay = true;
obj = hit.transform.gameObject.GetComponent("infoLibro");
obj.HoverIn();
}
} else {
obj.HoverOut();
hRay = false;
nameObj = "";
}
if(Input.GetTouch(0).tapCount == 2 && Physics.Raycast(ray, hit, 100)) if(hit.transform.gameObject.tag == "libro") obj.Click();
} else {
obj.HoverOut();
}
This script calls other functions belonging to components in the gameObject it's calling (HoverIn(), HoverOut() and Click()). It runs OK in PC using Remote, but when I do this in the very Android device the game freezes and crashes itself.
I imagined like there was a problem with the performance pertaining to Physics.Raycast(), but I also take in mind that the object type this snippet is calling (those with the "libro" tag) are around 3000 in total. They all have the same script in them (infoLibro).
I'm also including the code for those functions it's calling:
function HoverIn () {
shaderAnterior = renderer.material.shader;
if(esHueco) colorAnterior = Color.white;
ratonEncima = true;
//renderer.material.shader = Shader.Find("Unlit/Texture");
scriptInput.objetivo = gameObject;
scriptInput.objActivo = true;
if(scriptInput.estanteria) {
if(esHueco) {
renderer.material.color = Color.blue;
scriptEst.hueco = gameObject;
}
}
}
function HoverOut () {
ratonEncima = false;
/*
if(!esHueco) renderer.material.shader = shaderAnterior;
else {
renderer.material.shader = shaderAnterior;
renderer.material.color = colorAnterior;
}*/
activaEncima = false;
scriptInput.objetivo = null;
scriptInput.objActivo = false;
}
function Click () {
scriptWatcher = GameObject.Find("watcher").GetComponent(librosWatcher);
scriptPila = GameObject.Find("zonaPila").GetComponent(scriptZonaPila);
shaderAnterior = renderer.material.shader;
if(esHueco) colorAnterior = Color.white;
ratonEncima = true;
//renderer.material.shader = Shader.Find("Unlit/Texture");
scriptInput.objetivo = gameObject;
scriptInput.objActivo = true;
if(scriptInput.estanteria) {
if(esHueco) {
renderer.material.color = Color.blue;
scriptEst.hueco = gameObject;
}
}
var camaraActiva : GameObject;
camaraActiva = GameObject.Find("renderTexCam");
//if(Input.GetButton("Fire1") && ratonEncima && esPila && scriptPila.dentro1) {
if(esPila) {
//scriptWatcher.DestruirLibro(scriptWatcher.idLibroActual);
scriptWatcher.librosEstado[scriptWatcher.idLibroActual] = 1;
scriptZonaPila.libroEnMano = scriptWatcher.libro[scriptWatcher.idLibroActual];
scriptZonaPila.libroEnMano.Destroy(rigidbody);
scriptZonaPila.libroEnMano.transform.position = GameObject.Find("Bone04").transform.position;
scriptZonaPila.libroEnMano.transform.rotation = GameObject.Find("Bone04").transform.rotation;
scriptZonaPila.libroEnMano.transform.parent = GameObject.Find("Bone04").transform;
var pipi : infoLibro = scriptZonaPila.libroEnMano.GetComponent(infoLibro);
scriptZonaPila.libroEnMano.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");
//scriptZonaPila.libroEnMano.collider.enabled = false;
//libroScript = libroEnMano.GetComponent("infoLibro");
}
if(esHueco) {
scriptEst = GameObject.Find(receptorEs).GetComponent(triggerEstanteria); //hAhahHAhaHAaAhaAHhaAahAHaHAhhAHahHAhaHAaHAHahAhaAhaHAahAhaHAAHahAHahAHhah
activaEncima = true;
}
if(esHueco) {
//print("Ahmed");
if(activaEncima) scriptEst.ComprobarLibro();
}
}
Answer by PAEvenson · Jan 11, 2013 at 02:54 PM
You have some major issues with you implementation. First, the reason it runs fine on PC is because nothing under the if(Input.touches.Length > 0) will be called. Unless you have a touch monitor. The only place you set obj is here:
obj = hit.transform.gameObject.GetComponent("infoLibro");
so all of your obj.HoverOut(); calls will be throwing a nullReference Exception. If you are doing this 3000 times a frame that could be the issue.
I suggest getting it working with Input.Get$$anonymous$$ouseDown(0) before you switch it to handle touches
A little update: the first snippet of code runs on a single user object, not on the 3000 objects there. The thing is that now that I've modified the script accordingly, it works, but takes too much consumption of Overhead and Script CPU use in the device it slows it down to 15FPS.
It shouldnt...The issue is somewhere else. I have a game that does a couple hundred every frame with little to no frame rate difference. Check your log file.
A little more details on the issue right now:
The thing is that the 3000 books' (or gameObjects) are overloading the device's CPU with their Update() function. The problem is that now they still consume CPU having in Update() no script contents (just around 4 uncommented lines of code), and aside of the Overhead lag is a problem.
I don't know BTW why the Unity log isn't telling me anything from debugging the compiled debug app in the device except while it's compiling. I have the Development Build option turned on along with all those Build & Run settings, but all it tells while compiling the build is warnings about shaders (which have nothing to see with the scene I'm having this problem, they're on other scenes perhaps).
If the 3000 books have visual representation, it is far more likely that displaying the books is slowing things down than 3000 empty Update() calls. To test, turn off the renderer for the books and see what happens...or if you have Pro, run the profiler.
Answer by JMAA · Jan 18, 2013 at 12:51 PM
So I realized something was wrong: the raycast script needed to be on the user GameObject, not on the books.
But still, I get the crash on the device, maybe because of the rendering lag included in the CPU consumption. I tried changing the Occlusion mode to PVS, but I still get a lot of Overhead lag.