2D Toolkit Forum

2D Toolkit => Support => Topic started by: blameMike on September 19, 2013, 01:33:07 am

Title: Flickering Black Lines
Post by: blameMike on September 19, 2013, 01:33:07 am
I'm facing kind of a weird situation where my sprites seem to flicker when they stop moving. Black lines disappear and then reappear. I'm using a sprite collection and sprite animator on a GameObject with raycasters.

This video illustrates the issue. Any help would be greatly appreciated! Thanks in advance.

https://vimeo.com/74881319
Title: Re: Flickering Black Lines
Post by: JFBillingsley on September 19, 2013, 09:44:14 am
That'd be because your (smooth-scrolling) camera is often at non-integer coordinates. This can make tris appear one pixel bigger/smaller than they would otherwise due to rounding.

Try rounding off your camera's (and sprites'!) position to integer coordinates. Be sure to store the "real", floating-point position, and set the transform's position to the rounded version of that.

Unless there's a better way of doing that that I don't know about :Va
Title: Re: Flickering Black Lines
Post by: unikronsoftware on September 19, 2013, 11:32:03 am
That sounds pretty much the reason - another reason this could be more obvious than normally would be is your sprites arent being drawn 1:1 - this will likely cause issues with filtering and picking the correct pixel - you can try to avoid this by adding some padding or using premultiplied alpha.

Try JFBillingsley's suggestion first though. That will likely simply solve the problem straight away.
Title: Re: Flickering Black Lines
Post by: blameMike on September 19, 2013, 03:51:49 pm
Thanks so much for the replies. I will give those a try, and report back.
Title: Re: Flickering Black Lines
Post by: blameMike on September 20, 2013, 05:19:47 pm
If I round my camera coordinates I always get 0, and the camera doesn't following. I'm using a tk2d Camera, and this script to make it follow the character. Is there a better way to make it follow?


   void Update () {
      float yDelta = target.position.y - transform.position.y;
      float xDelta = target.position.x - transform.position.x;
      transform.Translate( Mathf.Round(xDelta * Time.deltaTime), Mathf.Round(yDelta * Time.deltaTime), 0.0f);
   }
Title: Re: Flickering Black Lines
Post by: unikronsoftware on September 20, 2013, 05:41:20 pm
That probably won't work.
Assuming your tk2dCamera is set to pixels per meter, you should do something like this.

Code: [Select]
Vector3 cameraPosition = ...;

Vector3 SnapToPixel( Vector3 position ) {
   float ppm = tk2dcam.CameraSettings.orthographicPixelsPerMeter;
   position.x = Mathf.Round(  position.x * ppm ) / ppm;
   position.y = Mathf.Round( position.y * ppm ) / ppm;
   return position;
}

void Update() {
     delta = ...;
     cameraPosition = cameraPosition + delta * Time.deltaTime;
     transform.position = SnapToPixel( cameraPosition );
}

cameraPosition contains the floating point information, but the transform.,position always contains the snapped position.
Title: Re: Flickering Black Lines
Post by: blameMike on September 20, 2013, 06:08:20 pm
Thanks so much. I'll give it a shot.  :)