player (the followed object) is shaking

hey, I’m trying to make a simple game just for practicing and I have tried to use cinemachine and it works but the player is shaking like he pushing the camera (you can see in the video). Does anybody have suggestions on how to fix it??

https://vimeo.com/441811130

or maybe this is because that code that I’ve attached to the camera??

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shootAndPoint : MonoBehaviour
{
    public GameObject crosshairs;
    public GameObject player;
    public GameObject bulletPrefab;
    public GameObject bulletStart;

    public float bulletSpeed = 60.0f;

    private Vector3 target;

    // Use this for initialization
    void Start()
    {
        Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {
        target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
        crosshairs.transform.position = new Vector2(target.x, target.y);

        Vector3 difference = target - player.transform.position;
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);

        if (Input.GetMouseButtonDown(0))
        {
            float distance = difference.magnitude;
            Vector2 direction = difference / distance;
            direction.Normalize();
            fireBullet(direction, rotationZ);
        }
    }
    void fireBullet(Vector2 direction, float rotationZ)
    {
        GameObject b = Instantiate(bulletPrefab) as GameObject;
        b.transform.position = bulletStart.transform.position;
        b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
        b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
    }
}

Can you show your CinemachineVirtualCamera inspector?

Also, when you use ScreenToWorldPoint, you have to be sure to call it after the camera has been positioned, or the result will be stale. The camera moves in CinemachineBrain.LateUpdate(), so your script must do ScreenToWorldPoint in LateUpdate (not Update), and you must set its script execution order to be after CinemachineBrain.

1 Like