- Home /
Unity smooth following 2D camera not working properly on different resolutions
I´m working in a third person 2D game with oblique camera view in Unity and I want the camera to follow the character smoothly. I´m using Vector3.lerp and works like a charm in mostly all the resolutions but when I change to 1080x720 or 600x480 (for example) it doesn´t work well.
I´m using an othographic camera which orthographic size changes depending on the resolution for pixel perfect. When I deactivate the pixelPerfect script doesn´t work in any of the resolutions. I post the pixelPerfect script:
[System.Serializable]
public class PerfectOverride
{
public int referenceOrthographicSize;
public float referencePixelsPerUnit;
}
public class PerfectPixel : MonoBehaviour
{
public int referenceOrthographicSize;
public float referencePixelsPerUnit;
public List<PerfectOverride> overrides;
private int lastSize = 0;
// Use this for initialization
void Start()
{
UpdateOrthoSize();
}
PerfectOverride FindOverride(int size)
{
return overrides.FirstOrDefault(x => x.referenceOrthographicSize == size);
}
void UpdateOrthoSize()
{
lastSize = Screen.height;
// first find the reference orthoSize
float refOrthoSize = (referenceOrthographicSize / referencePixelsPerUnit) * 0.5f;
// then find the current orthoSize
var overRide = FindOverride(lastSize);
float ppu = overRide != null ? overRide.referencePixelsPerUnit : referencePixelsPerUnit;
float orthoSize = (lastSize / ppu) * 0.5f;
// the multiplier is to make sure the orthoSize is as close to the reference as possible
float multiplier = Mathf.Max(1, Mathf.Round(orthoSize / refOrthoSize));
// then we rescale the orthoSize by the multipler
orthoSize /= multiplier;
// set it
this.GetComponent<Camera>().orthographicSize = orthoSize;
Debug.Log("Last Screen height "+lastSize + " Ortho Size" + orthoSize + " Multiplier " + multiplier + " PPU " + ppu);
}
// Update is called once per frame
void Update()
{
#if UNITY_EDITOR
if (lastSize != Screen.height)
{
UpdateOrthoSize();
GetComponent<CameraCenterController>().CameraChanged = true;
}
#endif
}
}
I must say that, when I use a fixed value as the interpolate in Vector3.lerp, instead Time.Deltatime, it works fine (but less smooth) in all resolutions but still have problems with 1080x720 and 600x480 working only in the Y axis and not in the X.
I paste my code for understanding:
public float speed;
public Vector3 distance;
private void FixedUpdate()
{
PlayerCentered();
}
void PlayerCentered()
{
Vector3 positionDesired = player.transform.position + distance;
Vector3 smoothPosition= Vector3.Lerp(transform.position, positionDesired, speed /*Time.Deltatime*/);
transform.position = smoothPosition;
}
I would like to know if you guys know why this is happening and if there is a solution. I research about this problem and everybody talks about lerp and smoothDamp but nobody seems to have problems when changing the resolution.
Thanx!
Your code is not dependent on your resolution at all. So if the code sometimes works but sometimes not, it's either not really because of the resolution, or there's additional code involved somehow. Your other script that sets your camera's orthographic size for example. Does that one involve transform.position or something?
Also, using or not using Time.deltaTime here should not have an effect other than changing the lerp speed. Since you're in a FixedUpdate context, Time.deltaTime has the constant value of the fixed time steo, which is 0.02 by default.
Oh, wow, I didn't know Time.deltaTime
returns Time.fixedDeltaTime
from within FixedUpdate()
! $$anonymous$$akes sense, but it is a bit obscure...
Anyway, as FlaSh-G said, the above code does not depend on resolution, it must be a different part of the code that affects the movement.
That totally makes sense so I deactivate my pixelPerfect script for testing. After that, none of the resolutions are working. The strange thing is that works perfect on the Y axis, follows the player and centers on it, but the X axis follows the player too slow so the character goes our of the screen at some point and the camera never centers on it.
In the next example you can see, in the first part, how I would like to behave and when I change the resolution, how bad behaves: https://www.youtube.com/watch?v=6rYTWFu-Jj8
How do you calculate distance
? In the video it is clearly visible that the camera moves to the side when you change the resolution, so I think that's what influences the camera position (could you share some code, please, preferably as an edit to the original question?). Do you have a CanvasScaler on the camera? That could explain why it works in y axis, but not the x.
Your answer
Follow this Question
Related Questions
Map level selection slide smoothly 3 Answers
Child GameObject is aligned to world axis and not the parents axis (UPDATE) 1 Answer
Smooth Camera 2D 0 Answers
smooth follow 2d camera runner ios 0 Answers
The sprite that is being followed by thr camera is very jittery and doesn't look very nice at all. 2 Answers