- Home /
Camera doesn't render sprites properly on 2D game
Hi everyone, I'm making a simple 2D sidescroll game with pixel art style sprites. The problem is that I can't make the ortographic camera render the moving sprites in a properly way; if the camera follows the player, the player sprite looks good when it's moving, but the background has an annoying jitter/rippling effect when the camera moves with the player, it's the same for the projectiles or moving enemies, they look weird, like with a forced and ugly motion blur effect. If I put the camera on the scene, not following the player, and with no scripts on it, the player has the same weird jitter effect when it's moving. The player sprite has a rigidbody2D with Interpolate checked, Filther mode Point, but it doesn't helped too much.
It seems some people have the same issue, like the the guy who put an example on this video: https://www.youtube.com/watch?v=9bNFnWYYNQ4&feature=youtu.be
from this post: http://answers.unity3d.com/questions/686494/most-unreplied-asked-question-why-blurry-effect-on.html
I uploaded two sample scenes, one with the camera following the player, and the other with a static camera: Following: https://mega.nz/#!o4AGlaoD!f55O0oGMFZKJEBaXtKsJ7A-vsP_l0h16U7hJaaCtjWI Not Following: https://mega.nz/#!tkAn0JrJ!kkyjCRHgybXgIjpoFmYvH4mgm0EXAffj2NUpE81f1_w
Here are the scripts.
For the camera:
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
public Transform player;
// public float speed;
//public float upperOffset;
//public float lowerOffset;
// public Vector3 screenSW;
//public Vector3 screenNE;
Vector3 offset;
private GameObject playerObject;
void Start()
{
transform.position = new Vector3(player.transform.position.x, player.transform.position.y, -10);
offset = transform.position - player.position;
}
public static float RoundToNearestPixel(float unityUnits, Camera viewingCamera)
{
float valueInPixels = (Screen.height / (viewingCamera.orthographicSize * 2)) * unityUnits;
valueInPixels = Mathf.Round(valueInPixels);
float adjustedUnityUnits = valueInPixels / (Screen.height / (viewingCamera.orthographicSize * 2));
return adjustedUnityUnits;
}
void LateUpdate()
{
Vector3 targetCamPos = offset + player.position;
transform.position = new Vector3(targetCamPos.x, targetCamPos.y, transform.position.z);
//Vector3 roundPos = new Vector3(RoundToNearestPixel(player.position.x, Camera.main), RoundToNearestPixel(player.position.y, Camera.main), transform.position.z);
//transform.position = roundPos;
}
}
For the player:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public bool grounded;
public float currentSpeed;
public float jumpforce;
public float gravityforce;
public float fireRate;
public float nextFire;
public Transform cannon;
public GameObject bullet;
public GameObject shield;
public GameObject shieldSprite;
private bool shieldUP;
private bool allowshoot;
public Transform groundCheck;
public float groundRadius = 0.2f;
public LayerMask whatIsGround;
public bool facingRight;
Vector2 movement;
Rigidbody2D playerRB;
void Start ()
{
currentSpeed = 0;
playerRB = GetComponent<Rigidbody2D>();
shield.SetActive(false);
shieldSprite.SetActive(false);
shieldUP = false;
facingRight = true;
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
float h = Input.GetAxisRaw("Horizontal");
playerRB.velocity = new Vector2(h * speed, playerRB.velocity.y);
if (Input.GetAxisRaw("Horizontal") < 0 && facingRight)
{
Flip();
}
if (Input.GetAxisRaw("Horizontal") > 0 && !facingRight)
{
Flip();
}
}
void Update()
{
Debug.Log("grounded = "+ grounded);
currentSpeed = playerRB.velocity.magnitude;
if (shieldUP == false)
{
allowshoot = true;
}
else
{
allowshoot = false;
}
if (Input.GetButtonDown("Jump"))
{
if (grounded)
{
jump();
}
}
if (Input.GetButton("Fire1"))
{
if (allowshoot)
{
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(bullet, cannon.position, cannon.rotation);
//audioSource.PlayOneShot(bulletSFX);
}
}
}
if (Input.GetButton("Fire2"))
{
if (!shieldUP)
{
shield.SetActive(true);
shieldSprite.SetActive(true);
shieldUP = true;
}
}
if (Input.GetButtonUp("Fire2"))
{
shield.SetActive(false);
shieldSprite.SetActive(false);
shieldUP = false;
}
}
void jump()
{
playerRB.AddForce(new Vector2(0,jumpforce));
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Your answer
Follow this Question
Related Questions
Why do my sprites/textures darken whenever I move my camera? 0 Answers
Blur on first plane 0 Answers
Why is 2d camera not smooth at 30 FPS 0 Answers
2D Camera Follow - Jittery? 1 Answer
2D camera rotating relatively to game object speed 0 Answers