Advertisement
osadsanu

Unity SmoothCamera2D-Doodle-Jump-Style

Nov 20th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SmoothCamera2D : MonoBehaviour {
  5.    
  6.     public float dampTime = 0.15f;
  7.     private Vector3 velocity = Vector3.zero;
  8.     public Transform target;
  9.     private Transform TargetOld;
  10.     private Vector3 InitPos;
  11.  
  12.     void Start(){
  13.         TargetOld = target;
  14.         InitPos = gameObject.transform.position;
  15.     }
  16.     void Update ()
  17.     {
  18.         if (target)
  19.         {  
  20.             if(target.transform.position.y > gameObject.transform.position.y){
  21.                 Vector3 point = camera.WorldToViewportPoint(target.position);
  22.                 Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
  23.                 Vector3 destination = transform.position + delta;
  24.            
  25.                 destination = new Vector3(InitPos.x,destination.y,InitPos.z) ;
  26.                 transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
  27.                 //transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z);
  28.        
  29.  
  30.  
  31.             }
  32.         }
  33.        
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement