Since I have started this platformer project I have used a pixel perfect script to help while I was developing it but now that the game has grown my pixel perfect script is getting in the way (illustrated with pictures)
My problem is with the zoom factor of the Pixel Perfect Script; I want the field of view to be appropriate for the player (not too close or far away)
This is zoom 1: too far away
This is zoom 2: too close (want the player to have a good view of enemies coming)
This is zoom 1.5 (I just added in the script:sunglasses:) : This is the field of view im looking for but it disorients the pixels because 1.5 zoom doesn’t fit with the pixel ratio calculations
So I am just wondering if anybody has advice for PixelPerfect in Unity in regards to my zoom problem.
Tuts, scripts, assets, etc… Any help will be AWESOME!
Heres the script for a reference (Made by unity user Marrt):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
namespace MoreMountains.CorgiEngine
{
//PixelPerfect Camera in Unity
//from Marrt
//Spriteguy gathered from http://www.rpgmakervxace.net/topic/7926-8-direction-sprites/
public enum FollowMode { SnapToScreenPixel, SnapToArtPixel };
public class PixelPerfect : MonoBehaviour
{
public static PixelPerfect instance; //make sure only one instance of this script exists
//PIXELPERFECT VIEWPORT
public Camera cam;
public Transform camAnchor;
private Transform followTarget;
public FollowMode snapMode = FollowMode.SnapToArtPixel;
private float orthoSize;
protected Transform _target;
//pixel perfect & zoom
public bool readAngle = true;
private float camTilt = 53.13010235F; //yields: zStretch 1.25F yStretch= 1.6666666666F for pixel perfect rendering
private float xStretch = 1F; //factor of X stretching is 1F
private float zStretch; //factor of Z stretching applied to floor Tiles to make pixels square
private float yStretch; //factor of Y stretching applied to wall Tiles to make pixels square
public float pxPerUnit = 100F; //one Unity MeterSpans pxPerUnit pixels
private float subPixelFactor = 2F; //changes with zoom Level, is used to position actor pefectly
//
//rounding Values, width of a single SCREEN-pixel in world coordinates, if you need ART-pixel snapping use RoundToArtPixelGrid()
private bool pixelSnapOn = true;
private float xSnap = 0F;
private float ySnap = 0F;
private float zSnap = 0F;
void Start()
{
Zoom(1.5F);
followTarget = GameManager.Instance.Player.transform;
}
void Awake()
{
instance = this;
InitViewPort(readAngle);
Zoom(1.5F);
}
void Update()
{
if (Input.GetKeyDown("1")) { Zoom(1F); }
if (Input.GetKeyDown("2")) { Zoom(1.5F); }
if (Input.GetKeyDown("3")) { Zoom(2F); }
if (Input.GetKeyDown("4")) { Zoom(3F); }
if (Input.GetKeyDown("5")) { Zoom(4F); }
if (Input.GetKeyDown("6")) { Zoom(5F); }
if (Input.GetKeyDown("7")) { Zoom(6F); }
}
private void InitViewPort(bool read)
{
if (!read)
{ //write tilt of cam
camAnchor.transform.rotation = Quaternion.Euler(new Vector3(camTilt, 0F, 0F));
}
//calculate stretch factors, they are 1 & infinity if environment is viewing from top
yStretch = 1F / Mathf.Cos(camAnchor.rotation.eulerAngles.x * Mathf.Deg2Rad);
zStretch = 1F / Mathf.Sin(camAnchor.rotation.eulerAngles.x * Mathf.Deg2Rad);
yStretch = float.IsInfinity(yStretch) ? 1F : yStretch;
zStretch = float.IsInfinity(zStretch) ? 1F : zStretch;
print("zStretch" + zStretch + "\tyStretch" + yStretch + "\nlenght of a pixel:" + 1 / pxPerUnit);
}
private void Zoom(float subPxFactor)
{
print("Pixel 1:" + subPxFactor + "x" + subPxFactor);
orthoSize = GetPixelPerfectOrthoSize(subPxFactor);
//StartCoroutine(ZoomTransition());
cam.orthographicSize = orthoSize;
}
private IEnumerator ZoomTransition()
{ //during transition, we cannot be pixelperfect
float timer = 1F;
while (timer > 0F)
{
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, orthoSize, Time.deltaTime * 10F);
timer -= Time.deltaTime;
yield return null;
}
cam.orthographicSize = orthoSize;
}
private float GetPixelPerfectOrthoSize(float screenPixelPerSpritePixelWidth)
{
subPixelFactor = screenPixelPerSpritePixelWidth; //1 means 1ArtPixel = 1 ScreenPixel, 2 means 1 Art = 2x2 Screen
float s = pxPerUnit * subPixelFactor;
xSnap = xStretch / pxPerUnit / subPixelFactor;
ySnap = yStretch / pxPerUnit / subPixelFactor;
zSnap = zStretch / pxPerUnit / subPixelFactor;
print("SceenPixelSnapingGrid:(" + xSnap + "," + ySnap + "," + zSnap);
return cam.pixelHeight / s / 2F;
}
public static Vector3 RoundToScreenPixelGrid(Vector3 worldPos)
{
float xSnapArt = PixelPerfect.instance.xSnap;
float ySnapArt = PixelPerfect.instance.ySnap;
float zSnapArt = PixelPerfect.instance.zSnap;
return new Vector3(Mathf.Round(worldPos.x / xSnapArt) * xSnapArt,
Mathf.Round(worldPos.y / ySnapArt) * ySnapArt,
Mathf.Round(worldPos.z / zSnapArt) * zSnapArt);
}
public static Vector3 RoundToArtPixelGrid(Vector3 worldPos)
{
float xSnapArt = PixelPerfect.instance.xSnap * PixelPerfect.instance.subPixelFactor;
float ySnapArt = PixelPerfect.instance.ySnap * PixelPerfect.instance.subPixelFactor;
float zSnapArt = PixelPerfect.instance.zSnap * PixelPerfect.instance.subPixelFactor;
return new Vector3(Mathf.Round(worldPos.x / xSnapArt) * xSnapArt,
Mathf.Round(worldPos.y / ySnapArt) * ySnapArt,
Mathf.Round(worldPos.z / zSnapArt) * zSnapArt);
}
//UI-Button Functions
public void PixelSnapOn(bool on)
{
pixelSnapOn = on;
}
public void ToggleSnappingMode(bool art)
{
if (art)
{
snapMode = FollowMode.SnapToArtPixel;
}
else
{
snapMode = FollowMode.SnapToScreenPixel;
}
}
}
}