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…

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);
			
			}
		}

I had the same issue and this pattern solved my problem. Hope this helps u out :slight_smile:

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;
		
	}

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.

For God’s sake, why u use Camera.main.ScreenToWorldPoint(pos);?
Use
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);