Hi, I’m currently building a 2D platformer for Android. I have a camera script that makes it follow the character between user entered screen bounds. The problem is that when I go to play the game and the camera starts following the character, the screen starts flickering and looks really bad. I think its either the sprites that cause this or the player.
I have read up a lot about this and people are saying to use FixedUpdate functions and LateUpdate, mess with Rigidbody Interpolate, change frame rate. None of these seem to work in the camera script or the player controller. I can’t pinpoint the exact problem Any help would be greatly appreciated
Thanks
How are you moving the camera? I know you mentioned this already but make sure any rigidbody2D calls are made in FixedUpdate() in your player script.
not sure if this will help but this is what im using :
[ExecuteInEditMode]
public class CameraController : MonoBehaviour
{
public bool IsFollowing { get; set; }
public Transform ChaseTarget;
public Vector2 Margin;
public Vector2 Smoothing;
private Vector3 shakeForce;
private float shakeStrength;
private float shakeDuration;
private Camera cameraComponent;
public void Start()
{
IsFollowing = true;
cameraComponent = GetComponent<Camera>();
}
void Update()
{
// set the desired aspect ratio (the values in this example are
// hard-coded for 16:9, but you could make them into public
// variables instead so you can set them at design time)
float targetaspect = 16.0f / 9.0f;
// determine the game window's current aspect ratio
float windowaspect = (float)Screen.width / (float)Screen.height;
// current viewport height should be scaled by this amount
float scaleheight = windowaspect / targetaspect;
// obtain camera component so we can modify its viewport
// if scaled height is less than current height, add letterbox
if (scaleheight < 1.0f)
{
Rect rect = cameraComponent.rect;
rect.width = 1.0f;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1.0f - scaleheight) / 2.0f;
cameraComponent.rect = rect;
}
else // add pillarbox
{
float scalewidth = 1.0f / scaleheight;
Rect rect = cameraComponent.rect;
rect.width = scalewidth;
rect.height = 1.0f;
rect.x = (1.0f - scalewidth) / 2.0f;
rect.y = 0;
cameraComponent.rect = rect;
}
if (shakeDuration > 0)
{
shakeForce = new Vector3(Random.Range(-shakeStrength, shakeStrength), Random.Range(-shakeStrength, shakeStrength), 0);
transform.position = transform.position + shakeForce;
shakeDuration -= Time.deltaTime;
if (shakeDuration < 0)
{
shakeDuration = 0;
}
}
}
void FixedUpdate()
{
float x = transform.position.x;
float y = transform.position.y;
if (IsFollowing && ChaseTarget != null)
{
if (Mathf.Abs(x - ChaseTarget.position.x) > Margin.x)
{
x = Mathf.Lerp(x, ChaseTarget.position.x, Smoothing.x * Time.deltaTime);
}
if (Mathf.Abs(y - ChaseTarget.position.y) > Margin.y)
{
y = Mathf.Lerp(y, ChaseTarget.position.y, Smoothing.y * Time.deltaTime);
}
}
transform.position = new Vector3(x, y, transform.position.z);
}
public void SetShake(float duration, float maxStrength)
{
shakeDuration = duration;
shakeStrength = maxStrength;
}
}
Thanks a lot for the reply, I had actually managed to fix it. It was something wrong with the textures tint from the mesh i was using from Tiled. It was a strange issue and I thought it was the camera flickering.
Thanks.