*
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class CameraMotionController : MonoBehaviour {
|
||||
public GameObject player;
|
||||
public float staticRectangleWidth = 200;
|
||||
public float staticRectangleHeight = 200;
|
||||
public bool drawDebug = true;
|
||||
private Camera camera;
|
||||
private Rect staticRect;
|
||||
public bool SpritePixels = true;
|
||||
public PixelPerfectCamera pixelController;
|
||||
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
camera = gameObject.GetComponent<Camera>();
|
||||
staticRect = new Rect();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void LateUpdate () {
|
||||
UpdateRectangle();
|
||||
MoveCameraToPlayer();
|
||||
}
|
||||
|
||||
void OnLevelWasLoaded(int level) {
|
||||
Debug.LogWarning("Loaded level: " + level);
|
||||
}
|
||||
|
||||
void MoveCameraToPlayer(){
|
||||
var pb = player.GetComponent<SpriteRenderer>().bounds;
|
||||
var playerRect = Tools.BoundsToRect(pb);
|
||||
|
||||
Vector3 offset = Vector3.zero;
|
||||
|
||||
var rightOffset = staticRect.xMax - playerRect.xMax;
|
||||
var leftOffset = staticRect.xMin - playerRect.xMin;
|
||||
|
||||
var topOffset = staticRect.yMax - playerRect.yMax;
|
||||
var bottomOffset = staticRect.yMin - playerRect.yMin;
|
||||
|
||||
if(rightOffset < 0){
|
||||
offset -= Vector3.right * rightOffset;
|
||||
}
|
||||
|
||||
if(leftOffset > 0){
|
||||
offset -= Vector3.right * leftOffset;
|
||||
}
|
||||
|
||||
if(bottomOffset > 0){
|
||||
offset -= Vector3.up * bottomOffset;
|
||||
}
|
||||
|
||||
if(topOffset < 0){
|
||||
offset -= Vector3.up * topOffset;
|
||||
}
|
||||
|
||||
transform.position += offset * Time.deltaTime * 5;
|
||||
|
||||
}
|
||||
|
||||
void UpdateRectangle(){
|
||||
float camUnitHeight = camera.orthographicSize * 2;
|
||||
float camPixelsToUnits = camUnitHeight / camera.pixelHeight;
|
||||
|
||||
|
||||
|
||||
float unitHW = staticRectangleWidth * 0.5f * camPixelsToUnits;
|
||||
float unitHH = staticRectangleHeight * 0.5f * camPixelsToUnits;
|
||||
|
||||
if(SpritePixels && pixelController != null){
|
||||
unitHW *= pixelController.zoom;
|
||||
unitHH *= pixelController.zoom;
|
||||
}
|
||||
|
||||
Vector2 center = transform.position;
|
||||
|
||||
staticRect.x = center.x - unitHW;
|
||||
staticRect.y = center.y - unitHH;
|
||||
staticRect.width = unitHW * 2;
|
||||
staticRect.height = unitHH * 2;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected(){
|
||||
if(staticRect == null){
|
||||
staticRect = new Rect();
|
||||
}
|
||||
if(camera == null){
|
||||
camera = gameObject.GetComponent<Camera>();
|
||||
}
|
||||
UpdateRectangle();
|
||||
if(drawDebug){
|
||||
|
||||
var pb = player.GetComponent<SpriteRenderer>().bounds;
|
||||
var rect = new Rect(pb.min.x, pb.min.y, pb.max.x - pb.min.x, pb.max.y - pb.min.y);
|
||||
//Tools.BeginGui(false);
|
||||
bool i = staticRect.Overlaps(rect);
|
||||
Tools.DrawRectangle(staticRect, i ? new Color(0,1,0,0.3f) : new Color(1,0,0,0.3f));
|
||||
//Tools.EndGui();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user