- Home /
2D sprite wavering
I have started creating a 2d game using unity. My player is moved by adding force to the rigidbody2d component attached. Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 5f;
private Animator anim;
private bool playerMoving;
public Vector2 lastMove;
private Rigidbody2D myRigidbody;
private static bool playerExists;
// Use this for initialization
void Start () {
anim = GetComponent <Animator>();
myRigidbody = GetComponent <Rigidbody2D> ();
if (!playerExists) {
playerExists = true;
DontDestroyOnLoad (transform.gameObject);
} else {
Destroy (gameObject);
}
}
// Update is called once per frame
void FixedUpdate () {
playerMoving = false;
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) {
myRigidbody.velocity = new Vector2 (Input.GetAxisRaw ("Horizontal") * moveSpeed, myRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0f);
}
if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) {
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, Input.GetAxisRaw ("Vertical") * moveSpeed);
playerMoving = true;
lastMove = new Vector2 (0f, Input.GetAxisRaw ("Vertical"));
}
if(Input.GetAxisRaw ("Horizontal") < 0.5f && (Input.GetAxisRaw ("Horizontal") > -0.5f))
{
myRigidbody.velocity = new Vector2 (0f, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool ("PlayerMoving", playerMoving);
anim.SetFloat ("LastMoveX", lastMove.x);
anim.SetFloat ("LastMoveY", lastMove.y);
}
}
All the sprites I've used are using the default sprite settings, except for the following changes:
- pixels per unit = 32
- filter mode = point (no filter)
- compression = none
(consistent with all sprites)
My problem is, once the character has stopped moving after moving in any direction, the sprites in my game appear to WAVER as they shift into focus.
This is really annoying, and any help would be appreciated!
$$anonymous$$y camera also has this script attached:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraController : $$anonymous$$onoBehaviour {
public GameObject followTarget;
private Vector3 targetPos;
public float moveSpeed = 5f;
private static bool cameraExists;
// Use this for initialization
void Start () {
if (!cameraExists) {
cameraExists = true;
DontDestroyOnLoad (transform.gameObject);
} else {
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
targetPos = new Vector3 (followTarget.transform.position.x, followTarget.transform.position.y, transform.position.z);
transform.position = Vector3.Lerp (transform.position, targetPos, moveSpeed * Time.deltaTime);
}
}
It will follow the character slightly behind its position, then catch up when the player stops moving.
usually the camera gets updated in LateUpdate, change that.
if it for not fix your wavering, please illustrate what you mean by that.
I tried that, no difference. What i mean by wavering is that the sprites around the character seem to jitter. Lines move across the screen (the same direction the player is moving) that distort the sprites and cause some of the pixels to compact into each other. But it occurs in precise lines.
Are you talking about pixel pefect? https://blogs.unity3d.com/2015/06/19/pixel-perfect-2d/
Also, if the problem is when the sprite should be still (thinking of each axis independently), maybe you can try to put its velocity back to 0 when below some custom threshold and there is no input for that axis.
Your answer
Follow this Question
Related Questions
Pixels per unit with tile palette? 1 Answer
How to make "circle" sprite’s border more smooth? 1 Answer
Crop/Mask a Sprite on runtime 0 Answers