- Home /
Camera.main.ScreenToWorldPoint in Perspective Camera
Hi. I have researched a lot and saw similar posts but I am not able to fix my problem. Any kind of help would really be appreciated. So I am developing a 2D game. Have touch ,Everything working perfectly fine. However I have decided to change camera to perspective for game. which is giving a perfect look to game. currently camera position is 0,-6.27,-6.32 and rotation 320,0,0.
My only problem is that my touch is not working now. I understand its due to z position problem for perspective camera. But tried everything but i am just not able to fix this. please help...
touch is not working. Lets say Input.mouse is also not working. Its because Camera.main.ScreenToWorldPoint can be used only for orthographic camera. And I want to use perspective cam. example below will work for orthocam perfectly.but not for perspective
Lets say ...
currTouchpos = Input.GetTouch(i).position;
//and now in Update()
Vector3 mouseRay = Camera.main.ScreenToWorldPoint(currTouchpos);
Vector2 touchPos = new Vector2(mouseRay.x, mouseRay.y);
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos)
{
//Do something
}
Answer by koushik.s · Aug 13, 2015 at 08:23 AM
You can use Raycast for detect the collision of the object
Try this code may help you Vector3 mousePosition = Input.mousePosition; mousePosition.z = 10f; RaycastHit2D hit;
Vector2 v = Camera.main.ScreenToWorldPoint(mousePosition);
Collider2D[] col = Physics2D.OverlapPointAll(v);
if(col.Length > 0){
foreach(Collider2D c in col)
{
Debug.Log("Collided with: " + c.collider2D.gameObject.name);
}
}
Thanks..But I tried my friend..But its not working. You see its 2D game and perspective camera. Consider my above comment as my code...
Hi koushik. Thanks and your code kind of seems to be working. When I hit gameobject debug gives around 8-80 logs for just one click. Basically I click on one gameobject and it actually click other gameobjects with same name on screen too.(I did Destroy(this.gameobject) on click.And found out when i click on one gameobject other objects gets detroyed too) I think I need to tell that my perspective camera is rotated. so using mousePosition.z = 10f is not gonna work right>?? Also we not using RacastHIt2D anywhere.
Answer by Nabeel Akhtar · Aug 13, 2015 at 09:14 AM
I had the same issue and this pattern solved my problem. Hope this helps u out :)
if (Input.touchCount > 0 && Input.GetTouch(0).phase != TouchPhase.Ended) {
touch = Input.GetTouch(0);
Vector2 pos = new Vector2(touch.position.x , touch.position.y);
Vector2 newPos = Camera.main.ScreenToWorldPoint(pos);
x = newPos.x;
y = newPos.y;
}
Thank you. But this wouldnt work. And also I have multiple touch in my game.
Answer by Suraj. · Aug 17, 2015 at 07:23 AM
OK. Thanks all for trying to help. BUt the only solution to this problem was to change colliders of gamobjects to 3d. and use physics.raycasthit instead of Camera.main.ScreenToWorldPoint.
Answer by chariot · Aug 17, 2015 at 07:30 AM
For God's sake, why u use Camera.main.ScreenToWorldPoint(pos);? Use var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);