Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

player (the followed object) is shaking

Discussion in 'Cinemachine' started by CrispyBamba, Jul 26, 2020.

  1. CrispyBamba

    CrispyBamba

    Joined:
    Jul 26, 2020
    Posts:
    1
    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??



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


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class shootAndPoint : MonoBehaviour
    6. {
    7.     public GameObject crosshairs;
    8.     public GameObject player;
    9.     public GameObject bulletPrefab;
    10.     public GameObject bulletStart;
    11.  
    12.     public float bulletSpeed = 60.0f;
    13.  
    14.     private Vector3 target;
    15.  
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.         Cursor.visible = false;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
    26.         crosshairs.transform.position = new Vector2(target.x, target.y);
    27.  
    28.         Vector3 difference = target - player.transform.position;
    29.         float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    30.         player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
    31.  
    32.         if (Input.GetMouseButtonDown(0))
    33.         {
    34.             float distance = difference.magnitude;
    35.             Vector2 direction = difference / distance;
    36.             direction.Normalize();
    37.             fireBullet(direction, rotationZ);
    38.         }
    39.     }
    40.     void fireBullet(Vector2 direction, float rotationZ)
    41.     {
    42.         GameObject b = Instantiate(bulletPrefab) as GameObject;
    43.         b.transform.position = bulletStart.transform.position;
    44.         b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
    45.         b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
    46.     }
    47. }
    48.  
    49.  
    50.  
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    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.
     
    Mortharos likes this.