How to check sprite's position within camera field of view?

I’m creating a sidescroller and for debugging purposes, I need to be able to check the sprite’s position within camera field of view (specifically, how far from the left edge of the window the character is) instead of in the world coordinates. Thus, using:

Debug.Log(playerTransform.position.x);

Doesn’t give me what I need. Neither does localPosition.x cause the sprite does not have any parent - is there any way to check it apart from parenting Camera to the player which I wouldn’t want to do cause quite often it does not move along with the player?

Vector2 screenPosition = Camera.main.WorldToViewportPoint(playerTransform.position);

This will give you the player’s position in the viewport. (0,0) is the bottom left of the viewport. (1,1) is the top right.

If you need screen pixels instead, use WorldToScreenPoint.

1 Like

Yep, that’s it, thank you very much :slight_smile: