Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

[Aporte] Camara 2D con targets

Iniciado por lightbug, Diciembre 19, 2016, 03:25:34 PM

Tema anterior - Siguiente tema
Diciembre 19, 2016, 03:25:34 PM Ultima modificación: Marzo 17, 2017, 03:38:33 AM por lightbug
Hola, siempre quise saber como funcionaban las camaras 2D que cuando llegabas a un cierto punto del nivel se quedaban quietas y/o modificaban alguna caracteristica como el target o el tamaño, etc. Cuando me sente dos segundos a pensar como era no podia creer que fuera tan facil de implementar.
 
 
 
Basicamente cuando el transform objetivo normal entra en un trigger2D que sea un nodo, el node se impone de target. Cuando sale el target vuelve a ser el target normal.
 
Para cada nodo se puede setear:
 
- tamaño ortografico (solo para proyeccion ortografica)
 
- FOV (solo para proyeccion perspectiva)
 
- lerp del ajuste de proyeccion
 
- lerp de seguimiento al target
 
- offsetX
 
- offsetY
 
 
 
camara 2D:
 
//Camera2D.cs ------------- (va en camara)
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Camera2D : MonoBehaviour {

struct CameraProperties
{
   public float lerpValue;
        public float projLerpValue;
        //orthographic
        public float size;
        //perspective
        public float fov;
        public float offsetX;
   public float offsetY;
   public Transform target;
}
//public
public float m_lerpValue = 2;
    public float m_projLerpValue = 3;
    public float m_size = 4;
    public float m_fov = 50;
    public float m_offsetX = 0;
public float m_offsetY = 0;
Transform m_target;
   
CameraProperties normalCam = new CameraProperties();
Transform m_transform;
Camera m_cam;
           
void Start () {
               
        m_transform = transform;
        m_cam = GetComponent<Camera>();
        normalCam.lerpValue = 2;
   normalCam.projLerpValue = 3;
   normalCam.size = 4;
        normalCam.fov = 50;
        normalCam.offsetX = 0;
   normalCam.offsetY = 0;
   normalCam.target =  Player.instance.transform;
   UseNormal();
        transform.position = new Vector3(m_target.position.x , m_target.position.y , transform.position.z);
}
public void UseOrthographicView(float lerpValue , float projLerpValue, float size, float offsetX , float offsetY , Transform target)
{
   m_lerpValue = lerpValue;
   m_projLerpValue = projLerpValue;
   m_size = size;
   m_offsetX = offsetX;
   m_offsetY = offsetY;
   m_target = target;
}
    public void UsePerspectiveView(float lerpValue, float projLerpValue, float fov, float offsetX, float offsetY, Transform target)
    {
        m_lerpValue = lerpValue;
        m_projLerpValue = projLerpValue;
        m_fov = fov;
        m_offsetX = offsetX;
        m_offsetY = offsetY;
        m_target = target;
    }
    public void UseNormal()
{
   m_lerpValue = normalCam.lerpValue;
        m_projLerpValue = normalCam.projLerpValue;
   m_size = normalCam.size;
        m_fov = normalCam.fov;
   m_offsetX = normalCam.offsetX;
   m_offsetY = normalCam.offsetY;
   m_target = normalCam.target;
}

 
void LateUpdate () {
 
   m_transform.position = new Vector3 (
      Mathf.Lerp(m_transform.position.x , m_target.position.x + m_offsetX, m_lerpValue * Time.unscaledDeltaTime),
      Mathf.Lerp(m_transform.position.y , m_target.position.y + m_offsetY, m_lerpValue * Time.unscaledDeltaTime),  
      m_transform.position.z);
        //Orthographic
        if (m_cam.orthographic)
            m_cam.orthographicSize = Mathf.Lerp(m_cam.orthographicSize, m_size, m_projLerpValue * Time.unscaledDeltaTime);
        else //Perspective
            m_cam.fieldOfView = Mathf.Lerp(m_cam.fieldOfView, m_fov, m_projLerpValue * Time.unscaledDeltaTime);            
       
       
         
    }
 
}

 
Y el nodo o trigger del target
 
CameraNode.cs
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Collider2D))]
public class CameraNode : MonoBehaviour {
   
    public float m_lerpValue = 2;
    public float m_projLerpValue = 2;
    [Header("Orthographic Size")]
public float m_size = 5;    
    [Header("Perspective FOV")]
    public float m_fov = 50;
    [Header("Offset")]
    public float m_offsetX = 0;
public float m_offsetY = 0;
Camera2D cam2D;
void Start()
{
        if (Camera.main.GetComponent<Camera2D>())
            cam2D = Camera.main.GetComponent<Camera2D>();
       
    }
    void OnDrawGizmos()
    {
        Gizmos.color = new Color(0, 0, 1, 0.1f);
        if (Camera.main.orthographic){
           
            Gizmos.DrawCube(transform.position + Vector3.right * m_offsetX + Vector3.up * m_offsetY,
                new Vector3(2 * m_size * Camera.main.aspect, 2 * m_size, 1));
        }
        else {            
            float distance = transform.position.z - Camera.main.transform.position.z;
            float verticalHalfSize = distance * Mathf.Tan(Mathf.Deg2Rad * (m_fov / 2));
            Gizmos.DrawCube(transform.position + Vector3.right * m_offsetX + Vector3.up * m_offsetY,
                new Vector3(2 * verticalHalfSize * Camera.main.aspect, 2 * verticalHalfSize, 1));
        }
       
    }
void OnTriggerEnter2D(Collider2D col)
{
   if (col == Player.instance.boxCollider) {
            if(Camera.main.orthographic)
      cam2D.UseOrthographicView (m_lerpValue , m_projLerpValue, m_size , m_offsetX , m_offsetY, transform);
            else
                cam2D.UsePerspectiveView(m_lerpValue, m_projLerpValue, m_fov, m_offsetX, m_offsetY, transform);
        }
}
void OnTriggerStay2D(Collider2D col)
{
        if (col == Player.instance.boxCollider)
        {
            if (Camera.main.orthographic)
                cam2D.UseOrthographicView(m_lerpValue, m_projLerpValue, m_size, m_offsetX, m_offsetY, transform);
            else
                cam2D.UsePerspectiveView(m_lerpValue, m_projLerpValue, m_fov, m_offsetX, m_offsetY, transform);
        }
    }
void OnTriggerExit2D(Collider2D col)
{
   if (col == Player.instance.boxCollider) {
      cam2D.UseNormal ();
   }
}
 
}

 
Saludos y espero que sirva
 

 
 
 
OJO!
 
--> En el codigo tengo "Player.instance.boxCollider", basta con referenciar cualquier collider a gusto
 
Agregué: 
 
- Funcionalidad de usar lo mismo para Orthographic y para Perspective.
 
- Gizmos para predecir la vista de la camara cuando utilice un target (para las dos proyecciones )
 
 



Etiquetas: