- Home /
Restricting player to camera FOV and scroll camera
Alrighty. I'm working on what is essentially a game like Ikaruga or 1943 -- a character moving along x and z axes along a level that's constantly moving. New to Unity so just figuring this all out as I go.
I'm hoping to do two things:
Have the camera continually pan across a level. It's not meant to look at the character or anything, just move along a fixed axis. I currently have it working, essentially, by using a Vector3.Lerp to move the camera from one point to another. I can't help but feel like there's a better way? Or maybe this is the best way.
Restrict the player's movement to within the camera's FOV. The player can't manually move outside of the camera view and is pushed along by it. In the longer term, if the player gets stuck between a wall and the camera edge, he dies. (for example, like the Super Mario Bros. 3 levels that are moving).
Can someone give me a starting point for what I would need to do here?
Answer by robertbu · Aug 04, 2013 at 04:04 PM
There are a couple of different ways you can handle this. Probably the easiest is to convert your character position to Viewport coordinates. You can use Camera.WorldToViewportPoint(). Viewport coordinates start at (0,0) in the lower left of the screen and go to 1,1 in the upper right. So an value less than zero or greater than one and the character's pivot point will be outside the view of the screen. You don't need it, but positive values of 'z' will be in front of the camera and negative values behind.
var v3 = Camera.main.WorldToViewportPoint(transform.position);
if (v3.x < 0.0) {
// Move him along
}
else if (v3.x > 1.0) {
// Hold him back
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613111523im_/https://answers.unity.com/themes/thub/images/avi.jpg)