포켓몬2D 블루 페어리 블루 만들 때 만든건데...카메라 시점이 따라 움직임 2D에서...

/*!  CameraFollow2D.cs

 * X-UniTMX: A tiled map editor file importer for Unity3d

 * https://bitbucket.org/Chaoseiro/x-unitmx

 * 

 * Copyright 2013-2014 Guilherme "Chaoseiro" Maia

 *           2014 Mario Madureira Fontes

 */

using UnityEngine;

using System.Collections;


namespace X_UniTMX.Internal

{

public class CameraFolow2D : MonoBehaviour

{

public float smoothMovement = 3.0f;

public Vector2 relativeDistance = Vector2.zero;

public string tagToFollow = "Player";

private GameObject playerRefence = null;

private Transform myTransform;


private void Awake()

{

myTransform = transform;

}


Vector3 GetPlayerPosition()

{

return new Vector3(playerRefence.transform.position.x + relativeDistance.x, // get X from player

playerRefence.transform.position.y + relativeDistance.y, // get Yfrom player

myTransform.position.z);  // get X from camera

}


void TryToFindPlayer()

{

// Obtem a instancia do objeto do jogador

playerRefence = GameObject.FindWithTag(tagToFollow) as GameObject;

if (playerRefence != null)

{

// Set current position camera to player

myTransform.position = GetPlayerPosition();

}

}


void Start()

{

TryToFindPlayer();

}


void Update()

{

if (playerRefence == null)

{

TryToFindPlayer();

if (playerRefence == null)

{

return;

}

}


Vector3 newPosition = GetPlayerPosition();


// Interpolate camera position to player

myTransform.position = Vector3.Lerp(myTransform.position, newPosition, smoothMovement * Time.deltaTime);

}

}

}




/*! CameraController.cs

 * X-UniTMX: A tiled map editor file importer for Unity3d

 * https://bitbucket.org/Chaoseiro/x-unitmx

 * 

 * Copyright 2013-2014 Guilherme "Chaoseiro" Maia

 *           2014 Mario Madureira Fontes

 */

using UnityEngine;

using System.Collections;


namespace X_UniTMX.Internal

{

public class CameraController : MonoBehaviour

{


public float PixelsPerUnit = 32.0f;

public Vector3 CamPos = Vector3.zero;

float ortographicSize;

public float Scale = 1.0f;

float scrollWheel;


// Use this for initialization

void Start()

{

CamPos = Camera.main.transform.position;

ortographicSize = Camera.main.orthographicSize;

Scale = 1;//Screen.height / 320.0f;

}


// Update is called once per frame

void Update()

{

ortographicSize = Screen.height / 2.0f / PixelsPerUnit / Scale;


if (Input.GetKey(KeyCode.W))

{

CamPos.y += 1 / PixelsPerUnit * 10;

}

if (Input.GetKey(KeyCode.S))

{

CamPos.y -= 1 / PixelsPerUnit * 10;

}

if (Input.GetKey(KeyCode.A))

{

CamPos.x -= 1 / PixelsPerUnit * 10;

}

if (Input.GetKey(KeyCode.D))

{

CamPos.x += 1 / PixelsPerUnit * 10;

}

scrollWheel = Input.GetAxis("Mouse ScrollWheel");

if (scrollWheel != 0)

{

Scale += scrollWheel;

if (Scale < 0.1f)

Scale = 0.1f;

}

Camera.main.transform.position = CamPos;

Camera.main.orthographicSize = ortographicSize;

}


void OnGUI()

{

GUI.Label(new Rect(0.95f * Screen.width, 0.01f * Screen.height, 0.1f * Screen.width, 0.1f * Screen.height), "Scale:");

Scale = GUI.VerticalSlider(new Rect(0.95f * Screen.width, 0.05f * Screen.height, 0.01f * Screen.width, 0.1f * Screen.height), Scale, 10.0f, 0.1f);

}

}

}