- Home /
Touch not working if camera is moved
So, the scenario is: I have a scene where the player can move the camera. There's non moving sprites in the scene. The camera can be panned around, allowing the player to touch on a sprite and activate its script. The touch function works as followed:
Vector3 pos = cam.ScreenToWorldPoint(TouchInputManager.Instance.GetTouchPosition());
Debug.Log(pos);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (hit.collider != null) {
GetTouchPosition returns:
return new Vector2(Input.mousePosition.x, cam.pixelHeight - Input.mousePosition.y);
The problem is, the Box Collider isn't being hit in certain areas around each sprite. If the camera is moved, and let's say, the sprite is near the edge of the screen for instance, even if I touch where it exactly its box collider is, nothing happens. It only detects a hit if I pan the camera back to close its original position. My theory is that the positions from touch to screen to world aren't being interpreted properly for the situation of a moving camera. And yes, the 2D box collider matches the size of the object sprite.
Your answer
Follow this Question
Related Questions
camera smooth follow the targets after touch buttons 0 Answers
Rotate camera smooth on touch 0 Answers
Image effects disabling camera touch script 1 Answer
ScreenToWorldPoint fixed height 1 Answer
perspective camera into android 0 Answers