- Home /
Sprites around my player is shaking when the player is moving
All of my sprites are 32x32 pixels. A camera is following my player and everytime my player is moving, the other sprites that are seen in the camera are shaking/shuddering. When my player is moving up, the sprites are shaking up and down.
I already have all my shaders pixel snapped. I've also tried it without pixel snapping. I also tried making my sprites Bilinnear, Trillinear, and Point, and none of those three fixed the problem.
I'm also not using a code for my camera to follow the player. I just have my player as the parent of the camera, so the player is always pixel perfect even when I'm moving since I'm always at the center, but the other sprites around me are shuddering.
My player moves with rigidbody.velocity. And it works just fine with my player. EDIT: My player moves with rigidbody2D.velocity.
edit/additional info: The shaking only appears on the sprites that are not moving. The other sprites around me (the non-player sprites) have a code that makes them move by themselves. Whether the player/camera is moving or not, the sprites who are moving with rigidbody.velocity (the same as my player's) doesn't shake or jitter.
I've googled EVERYWHERE about my problem, and NONE worked so far. The sprites around the player just keeps jittering when the player/camera is moving.
Please help. My boss is going to fire me and murder me afterwards for wasting his time. I hope I explained everything clearly. Thanks! :)
ANOTHER EDIT:
I tried to use this function:
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; }
With this code in the Update() function:
Vector3 roundPos = new Vector3(RoundToNearestPixel(p.transform.position.x,
Camera.main),RoundToNearestPixel(p.transform.position.y, Camera.main),-31); transform.position = roundPos;
And the sprites around me stopped shaking! BUT then, it was my player now that started shaking. Everytime I move, my player started to shake just like how the other npcs around me shaked without that code above. How can I fix this? I want all sprites to stop shaking when I'm moving.
And yeah, I think I know why it's shaking, because I know how the code above works. It's just that I don't know how to fix it.
NEW EDIT/UPDATE: I somehow made the shaking of my player less visible by having its shader pixel-snapped. But it's still there. And when I'm moving left or right, there are black lines that appear and disappear at the end of his face. So his face grows longer in width with that black line and goes back to original everytime I move.
So yeah, it's clearly less visible when I pixel snap it, but it's still there.
Thanks :)*
EDIT:
Nevermind guys. My employer just randomly decided to find another programmer. (In other words, he fired me.) So I guess I won't need an answer for this problem anymore. But still, thanks for all your time and help! :)
It sounds like the problem is somewhere in the code you are using to move your player, could you post it?
Btw if you are doing a a 2D game without physics, I strongly recommend that you dont use rigidbody to move your player, as it gives lot of headaches.
Here's the code I'm using to move my player, which also moves the camera since my camera is a child of the player's gameobject.
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
rigidbody.velocity = Vector3.up;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)){
rigidbody.velocity = Vector3.left;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
rigidbody.velocity = Vector3.right;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow)){
rigidbody.velocity = Vector3.down;
}
if(!Input.Any$$anonymous$$ey){
rigidbody.velocity = Vector3.zero;
}
I also tried moving my player with transform.Translate, and that doesn't change anything at all.
I don't quite understand which object's position you are setting to the roundPos. But I remember I once had a similar problem where my player sprite was shaking. However, I made the camera following the player by script. The problem was that I used the Update function. After I used LateUpdate it worked. So depending on what you are doing with your roundPos maybe LateUpdate would be of any help.
@$$anonymous$$artin1 Both the function and the roundpos code is in the camera function. And I removed the Camera object from the player's children (since the code already follows the player). I tried using LateUpdate but it's still shaking. I've tried LateUpdate in every code I have that involves moving and stuff. None worked.
The p gameobject by the way is the player gameobject.
This isn't really the answer, I can try look into it more when I have time...
However I would think the shaking is most likely floating point errors / rounding errors.
Try too $$anonymous$$imize the times when you are going for float to int and back to float again.. that kind of stuff.
Answer by Leucaruth · Jul 03, 2015 at 11:09 PM
Try to avoid using RigidBody with sprites, use RigidBody2D instead.
As for the movement, remove the RigidBody and try this
private float hor, ver;
public float speed = 1;
void Update () {
hor = Input.GetAxisRaw("Horizontal");
ver = Input.GetAxisRaw("Vertical");
transform.Translate((hor * Vector2.right + ver * Vector2.up ) * Time.deltaTime * speed);
}
It works well for me at least.
As a side note, you should be careful, if you are using plain sprites in a solid color background, you will notice the jittering a lot, specially if you are moving in the X axis. This is normal the way the refresh of the screen works, if you want to solve this, you should configure the V-Sync in the quality settings to your needs.
I forgot to say, I am indeed using Rigidbody2D. I'm sorry I didn't put it in my question. I also already tried turning V-Sync On or Off, and none worked.
Well, im running out of options. Are you using a laptop or have you tested to build the project and run it in full screen mode?
I'm using a desktop but I've tried it on a laptop as well and the problem is also visible there. I've also tried both windowed and fullscreen mode.
I recently just ran out of options too, which is why I posted this question.
Thanks for the help though :)
Answer by dazman76 · Jul 04, 2015 at 04:17 PM
I get the feeling this is happening because you're affecting Physics objects, and you're using Update(). If you do anything at all with RigidBody, Collider or other physics components, you must do that in FixedUpdate(). Additionally, switch any multiplication with Time.deltaTime to use Time.fixedDeltaTime for any code you move to FixedUpdate().
Hopefully, your jittering will be gone :) I think the camera / player parenting is a red herring.
PS - FixedUpdate() is generally going to be better than LateUpdate(), when it comes to modifying Physics components. Physics processing for the current frame is already finished when LateUpdate() is called, so any changes you make to physics objects within LateUpdate() won't apply until the next frame - because no further processing will be applied to them by the physics engine.
EDIT:
This page in the Unity docs explains the per-frame events/timeline, and there's a diagram at the bottom that shows the whole thing - damn useful :)
I've already tried FixedUpdate as well on my movement script, and nothing changed. And I'm directly changing the rigibody's velocity so I actually expected nothing to change.
When I press the UpArrow key once, it sets my rigidbody2d to vector3.up once. And when I'm not pressing any key, I set it back to vector3.zero once. So since It's only happening once, I don't think it will matter whether it's in FixedUpdate or Update.
And yeah, I think I already know what LateUpdate does, and its difference between the three kinds of Updates. I also know about ExecutionOrders and stuff.
Thanks anyways though :)