public Vector2 margin, Smoothing;
public BoxCollider2D bounds;//2d背景物理box
Vector3 min, max;
public Transform player;
Camera camera;
void Start () {
min = bounds.bounds.min;
max = bounds.bounds.max;
camera = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
var x = transform.position.x;
var y = transform.position.y;
if (Mathf.Abs(x-player.position.x)>margin.x)
{
x = Mathf.Lerp(x, player.position.x, Smoothing.x * Time.deltaTime);
}
if (Mathf.Abs(y-player.position.y)>margin.y)
{
y = Mathf.Lerp(y, player.position.y, Smoothing.y * Time.deltaTime);
}
var halfwidth = camera.orthographicSize * ((float) Screen.width / Screen.height);
x = Mathf.Clamp(x, min.x + halfwidth, max.x - halfwidth);
y = Mathf.Clamp(y, min.y + camera.orthographicSize, max.y - camera.orthographicSize);
transform.position = new Vector3(x, y, transform.position.z);
}