Advertisement
NightFox

Sep 28 2013

Sep 27th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraController : MonoBehaviour {
  5.    
  6.     // Define el objetivo al que sigues
  7.     public GameObject target;
  8.    
  9.     // Parametros del seguimiento
  10.     public float distance, height, angle;
  11.    
  12.    
  13.     // Control de posicion de la camara
  14.     private Vector3 position;
  15.  
  16.     // Use this for initialization
  17.     void Start () {
  18.        
  19.         // Deten la ejecucion en caso de error de target
  20.         if (target == null) Debug.LogError("Target no asignado");
  21.        
  22.     }
  23.    
  24.     // Update is called once per frame
  25.     void LateUpdate () {
  26.        
  27.         // Mueve la camara
  28.         MoveCamera();
  29.    
  30.     }
  31.    
  32.    
  33.     // Posicion la camara detras del objetivo
  34.     void MoveCamera() {
  35.        
  36.         // Calcula la altura
  37.         position.y = (target.transform.position.y + height);
  38.        
  39.         // Calcula la posicion detras del target
  40.         angle = (Mathf.Deg2Rad * (target.transform.localEulerAngles.y - 180));
  41.         position.x = ((Mathf.Sin(angle) * distance) + target.transform.position.x);
  42.         position.z = ((Mathf.Cos(angle) * distance) + target.transform.position.z);
  43.        
  44.         // Coloca la camara
  45.         this.transform.position = position;
  46.        
  47.         // Haz que la camara mire al target
  48.         this.transform.LookAt(target.transform.position);
  49.        
  50.     }
  51.    
  52.    
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement