- Home /
how to fix jitter on a parallax?
I'm pretty new to Unity. I recently used the tutorial by code monkey found here on how to make a parallax background. It kind of works, but for some reason it is jittering. it didn't seem to jitter in the video so I'm a little bit confused. is there any way I could add a smoothing effect to the code?
[SerializeField] private Vector2 parallaxEffectMultiplier;
private Transform cameraTransform;
private Vector3 lastCameraPosition;
private void Start()
{
cameraTransform = Camera.main.transform;
lastCameraPosition = cameraTransform.position;
}
private void Update()
{
Vector3 deltaMovement = cameraTransform.position - lastCameraPosition;
transform.position += new Vector3(deltaMovement.x * parallaxEffectMultiplier.x, deltaMovement.y * parallaxEffectMultiplier.y);
lastCameraPosition = cameraTransform.position;
}
}
please help
Tell me, what is the code you use on "Player" to "Move" around??
I already implemented the parallax code and it works fine. thanks so much :D
Answer by UnityM0nk3y · Apr 05, 2021 at 09:29 AM
Here is my "Parralax 2d Code" I use, it has "smoothing":
Transform cam; // Camera reference (of its transform)
Vector3 previousCamPos;
public float distanceX; // Distance of the item (z-index based)
public float distanceY;
public float smoothingX = 1f; // Smoothing factor of parrallax effect
public float smoothingY = 1f;
void Awake()
{
cam = Camera.main.transform;
}
void Update()
{
if (distanceX != 0f)
{
float parallaxX = (previousCamPos.x - cam.position.x) * distanceX;
Vector3 backgroundTargetPosX = new Vector3(transform.position.x + parallaxX, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, backgroundTargetPosX, smoothingX * Time.deltaTime);
}
if (distanceY != 0f)
{
float parallaxY = (previousCamPos.y - cam.position.y) * distanceY;
Vector3 backgroundTargetPosY = new Vector3(transform.position.x, transform.position.y + parallaxY, transform.position.z);
transform.position = Vector3.Lerp(transform.position, backgroundTargetPosY, smoothingY * Time.deltaTime);
}
previousCamPos = cam.position;
}
Your answer
Follow this Question
Related Questions
Is this the best way to do an AutoParallax Background ? 1 Answer
trouble with getting the background to show past the midground in 2d parallax backgrounds 0 Answers
Smoothen Mouse Axis Input 2 Answers
2D Parallax 1 Answer
Parallax effect for background [C#] just a simple math error but I can't fix it 1 Answer