• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Obi02_ · Feb 09, 2018 at 06:16 AM · 2dsprite2d gamescript.sprites

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!

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Obi02_ · Feb 09, 2018 at 10:31 PM 0
Share

$$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.

avatar image hexagonius · Feb 09, 2018 at 11:20 PM 0
Share

usually the camera gets updated in LateUpdate, change that.
if it for not fix your wavering, please illustrate what you mean by that.

avatar image Obi02_ hexagonius · Feb 09, 2018 at 11:53 PM 0
Share

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.

avatar image Meguia Obi02_ · Feb 10, 2018 at 12:56 AM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pixels per unit with tile palette? 1 Answer

How to make "circle" sprite’s border more smooth? 1 Answer

How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers

How to determine the direction of a 2D sprite on a Y as Z Tilemap 0 Answers

Sprite disappears on android 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges