Skip to content

Instantly share code, notes, and snippets.

@dschneider
Created March 14, 2014 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschneider/9551985 to your computer and use it in GitHub Desktop.
Save dschneider/9551985 to your computer and use it in GitHub Desktop.
Unity 2D Camera movement
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
private Transform target;
// Use this for initialization
void Start () {
this.target = GameObject.Find ("Player").transform;
}
// Update is called once per frame
void Update () {
Vector3 point = camera.WorldToViewportPoint(target.position);
float cameraMovementX = 0.5f;
float cameraMovementY = 0.5f;
if ((target.position.x - 3f) < 0) {
cameraMovementX = point.x;
}
if ((target.position.y - 2.8f) < 0) {
cameraMovementY = point.y;
}
Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(cameraMovementX, cameraMovementY, point.z));
Vector3 destination = transform.position + delta;
this.transform.position = Vector3.SmoothDamp (this.transform.position, destination, ref velocity, dampTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment