Created
March 14, 2014 17:00
-
-
Save dschneider/9551985 to your computer and use it in GitHub Desktop.
Unity 2D Camera movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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