- Home /
The question is answered, right answer was accepted
Touch interface not working
I was trying to convert my application to mobile, and have encountered a serious problem with the touch interface (Using Raycast). While working very well with a normal scene (picking objects ecc), when implementing the exact same code into a scene that imports asset bundles, it stops working. Touch works on GUI items, but not on the 3D objects I need it to work on. Here's my code:
#pragma strict
var Target : GameObject;
var TargetGrabFunction : GrabNRotate;
private var cam : Camera;
function Start(){
cam = Camera.main;
}
function FixedUpdate(){
if (Input.touchCount > 0)
{
var theTouch : Touch = Input.GetTouch (0);
var ray = cam.ScreenPointToRay(theTouch.position);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,100))
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Began)
{
Target = gameObject.Find(hit.collider.gameObject.name);
TargetGrabFunction = Target.GetComponent(GrabNRotate);
//using 0, 1 and 2 for my boolean cos I need 3 states of true and false
if((TargetGrabFunction.bringOver==0 || TargetGrabFunction.bringOver==2))
TargetGrabFunction.bringOver = 1;
else
TargetGrabFunction.bringOver = 2;
}
}
}
}
}
Basically, this code is supposed to enable another external code on the gameobject when it is touched. It works fine on an empty scene with only the gameobject.
Why are you using gameObject.Find()? You already have a reference to the GameObject (hit.collider.gameObject) that you can simply assign... This might be the cause of you problem as well btw. (multiple gameobjects with the same name)
tried to change it, didn't work. maybe I wrote it wrong? Target = hit.collider.gameObject;
Nah, that looks fine to me. What exactly does not work? You should at least get a valid gameObject in any case since a collider can't exist without one and your raycast did hit something.
As I said, when in an empty test scene with only 1 object, the object gets hit and the script works. When I use the same scripts on the same object (a simple cube, for the test) in a scene full with other objects and scripts and stuff, and assetbundles downloading in the background, it doesn't work. Thing is that I'm doing it on my galaxy tab 10.1, and my computer is a $$anonymous$$ac, so I can't connect a cable to use the profiler to debug.
Well, you could try and replace the touch specific code with normal mouseInput (Input.Get$$anonymous$$ouseButtonDown(0) and Input.mousePosition) so you can debug it in the unity editor to make sure that the function itself is working correctly in your scene (or find out what the problem is if it's not). You can use compiler directives for platform dependent compilation to have both in your code at the same time ( http://unity3d.com/support/documentation/$$anonymous$$anual/Platform%20Dependent%20Compilation.html ).
Answer by yusufulutas · Jun 05, 2012 at 07:21 AM
first you use different layers. for examle: "default" for first camera , "minimap" for second camera and you do "culling mask = default" for first camera and "culling mask = minimap" for second camera. if you dont use skybox, you can "clear flag = depth only" and you must ray send for every camera
cool, i didn't think of layers. i'll try that, shukran!
Follow this Question
Related Questions
Raycast on touch 3 Answers
How to touch select 3D objects 2 Answers
Make object block a raycast 1 Answer
Continuous detection of object using ray cast 2 Answers
select and drag an object with touch 1 Answer