0

I am working in a 2d platform game I applied cinemachine camera and parallax script to give a good effect ,but the parallax is shaking and vibrating hard , I found out that the cinamchine was the reason because the camera it shaking, when I disabled the cinemachine it work smoothly

here is the parallax code

    private float startpos;
private GameObject cam;
[SerializeField] private float parallax;
[SerializeField] private float speed = 0.1f;

// Start is called before the first frame update
void Start()
{
    cam = GameObject.Find("Main Camera");
    startpos = transform.position.x;
}

// Update is called once per frame
void Update()
{
    float distance = (cam.transform.position.x * parallax);
    transform.position = new Vector3(startpos + distance, transform.position.y, transform.position.z);
}

and the settings of the MC vcam1 enter image description here

please any help I dont find any on with that problem

2 Answers2

1

You could try setting the CinemachineBrain update method to Manual Update and then just calling ManualUpdate() in another script.

using UnityEngine;
using Cinemachine;

public class ManualBrainUpdate : MonoBehaviour
{
    public CinemachineBrain cineBrain;
    public float smoothTime;

    private void Start()
    {
        cineBrain = GetComponent<CinemachineBrain>();
    }

    void Update()
    {
        cineBrain.ManualUpdate();
    }
}
0

To avoid any kind of jitter in Unity (like what the camera is adding to distance when it shakes) use the various SmoothDamp functions e.g. https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html:

public float smoothTime = 0.3f; // adjust this to taste
private float distance;
private Vector3 velocity = Vector3.zero;
private Vector3 targetPos

void Update()
{
    distance = cam.transform.position.x * parallax;
    targetPos = new Vector3(startpos + distance, transform.position.y, transform.position.z)
    transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);
}
Absinthe
  • 3,258
  • 6
  • 31
  • 70
  • thank you its really helpful to causes the lag a bit and that is something a dont want but its significantly better – abdulrhman ahmed Jun 21 '22 at 22:28
  • You're welcome. Adjust the `smoothTime` if you feel it's laggy. You want to set it so you get a balance between smoothness and speed of movement. – Absinthe Jun 22 '22 at 07:58