- Home /
Novice problem on Android with Touch position and object world placement. Help!
Hi, I am just trying to spawn a little cube in my scene where I touch the screen. But the behavior i get is the following:
As you can see it seems that whatever i get from ScreenToWorldPoint is centered around some sort of center (maybe the camera?) and also the input seems to be tiny (thats why i multiply the 2 touch inputs by 30 (i.e. touch.position.x*30) to be able to see the different cubes drawn on radically different places as I press different screen points (before the multiplication all of the cubes where drawn at the same spot more or less)
Seems like I am doing something wrong coordinate system wise, but i dont get what. Any help will be greatly appreciated, thanks!!!
function DebugCheckInput () {
while(true){
if(Input.touchCount >=1) {
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
//Camera.main.nearClipPlane
touchPos3D = Camera.main.ScreenToWorldPoint(Vector3(touch.position.x*30, touch.position.y*30, Camera.main.nearClipPlane));
Manager.use.SpawnDebugBlock (touchPos3D.x, touchPos3D.y);
}
else if (touch.phase == TouchPhase.Moved){
}
else if (touch.phase == TouchPhase.Ended)
{
}
}
yield;
}
}
function SpawnDebugBlock (xPosIn : float, yPosIn : float) {
// just to see where i am touching, if
var debug_cubes = gameObject.FindGameObjectsWithTag("Cube");
Instantiate (debug_cubes[0], Vector3(xPosIn, yPosIn, 0.0), Quaternion.identity);
}
Answer by vaggLivaditis · Aug 21, 2011 at 11:55 AM
Hmm I seem to have a problem with the image, the url is: http://www.freeimagehosting.net/102af
Answer by vaggLivaditis · Aug 21, 2011 at 01:48 PM
Damn I just found what was happening, seems that the problem was the Camera.main.nearClipPlane, as soon as i put 16.0 (my cameras distance to where i want the cube to spawn) all is ok. Strange function though, I would expect that the screen space is where the camera is
Answer by crazypit · Dec 07, 2011 at 06:40 PM
Hello,
I have the same problem but it does not seem to go away with correct z setting. I also get some coordinates that are centered around my current camera position. Nevertheless, these coordinates have nothing to do with correct World coordinates. Did you find a solution to this?
Your answer
Follow this Question
Related Questions
iPad touch is offset! Unity2d. 2 Answers
Instantiate Prefab 1 Answer
Touch.ScreenPosition doesnt work ? 0 Answers