How to make a camera-window in Unity3D

Hello everybody,

I am programing a 2.5 D Spaceshooter game. Unfortunately, I have some problems with the camera.

With this code, my camera follows only the spaceship.

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

public GameObject player;

  private Vector3 offset;

  void Start ()
  {
      offset = transform.position - player.transform.position;
  }

  void LateUpdate ()
  {
      transform.position = player.transform.position + offset;
  }
}

The camera should slightly follow the spaceship but should not go out of the boundary like this video:

On the website:Game Platforms recent news | Game Developer There is a example with Curb Camera Motion. I want to make a camera-window which pushes the camera position as the player hits the window edge.

Any ideas, how to realize this?

You only need a few additions to your script. Speaking of it in pseudocode, essentially you want to calculate the lateral offset between your space ship and your camera (most likely the X axis) and as long as it is within a limited amount (your window), then don’t move the camera laterally.

If the lateral offset gets too large (say spaceship too far right, camera too far left), then you move the camera to follow the space ship lagging left by the maximum window.

You have alternate logic to sense the other side, if the spaceship gets too left and the camera too right. With some clever coding the same snippet of code could probably handle both.

Thanks for the answer.
I have this code now. But unfortunately, this code does not work 100% correct. Any improvements?

public class CameraController : MonoBehaviour {

      public GameObject player;
      public Vector3 min;
      public Vector3 max;

      private Vector3 offset;

      void Start ()
      {
          offset = transform.position - player.transform.position;
      }

      void LateUpdate ()
      {
          //transform.position = player.transform.position + offset;

          float step = 3 * Time.deltaTime;

          Vector3 newPos = player.transform.position + offset;
          newPos.x = Mathf.Clamp(player.transform.position.x, min.x, max.x);
          newPos.y = Mathf.Clamp(player.transform.position.x, min.y, max.y);
          newPos.z = Mathf.Clamp(player.transform.position.x, min.z, max.z);
          transform.position = Vector3.MoveTowards(transform.position, newPos, step);//newPos;

      }
  }