Search Unity

Question Cinemachine choppy when i rotate the object its following

Discussion in 'Cinemachine' started by ODYDEV, Jun 23, 2022.

  1. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    I'm trying to make a follow camera for this little rocket game, but whenever the rocket moved the camera was choppy, so I looked up why it did that and I found a forum post saying something about damping. So i turned off damping and it was fine when it moved, but when it rotated it was choppy again. Could I get some help?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Can you please post an image of your vcam inspector, taken while the game is playing?
     
  3. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    upload_2022-6-23_16-33-28.png
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Maybe we should back up a little, because looking at your inspector I don't know what you're trying to do so it's hard for me to help.

    Can you give some game context? (i.e. 2D, 3D, what kind of game)
    Can you describe how the rocket is moving?
    Can you describe how you would like the camera to move?
     
  5. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    This is a 3D game with low gravity, the rocket is rotating with the A and D keys and it moves in the direction that it is pointing when I press space. I use Rigidbody for movement and transform.Rotate for rotating along the z-axis. I would like the camera to move along the x-axis in the direction the rocket goes.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Try changing your Binding Mode to World Space.

    upload_2022-6-23_16-47-46.png
     
  7. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    That has not appeared to have changed anything. It is still very choppy.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Maybe I don't understand what you mean when you say "choppy". A video would help.

    You mentioned that you are using RigidBody to drive your rocket. You might be facing a FixedUpdate vs Update inconsistency. That would give a certain kind of choppiness. I had asked you to show the vcam inspector when the game was playing. This is because when it is playing, the vcam update mode is indicated to the right of the Solo button on the inspector. What does it say?

    Also, can you show an image of the CinemachineBrain inspector?
     
  9. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    how do I send you a video on here?
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    You can paste a gif, or you can upload it somewhere and paste a link
     
  11. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Thanks. It does look like an update clock issue. I still didn't see the vcam inspector while the game was playing.
    Try enabling Interpolation on the rocket's RigidBody.
     
  13. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    I turned on interpolation but it's still choppy.
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    ok, I can see that the vcam is updating in LateUpdate. I think the cameras are set up correctly.
    How are you moving the rocket? Can you show the code?
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Hang on, I think the problem is here. You must use the RigidBody exclusively for moving the rocket. Never touch the transform directly. Just add forces to the RB, otherwise interpolation won't work properly.
     
  16. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    This is my movement script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour {
    6.     [SerializeField] float mainThrust = 0;
    7.     [SerializeField] float rotationThrust = 0;
    8.     [SerializeField] AudioClip mainEngine;
    9.  
    10.     [SerializeField] ParticleSystem mainThrusterParticles;
    11.     [SerializeField] ParticleSystem leftThrusterParticles;
    12.     [SerializeField] ParticleSystem rightThrusterParticles;
    13.    
    14.     Rigidbody rb;
    15.     AudioSource audioSource;
    16.  
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.         audioSource = GetComponent<AudioSource>();
    21.     }
    22.  
    23.     void Update() {
    24.         ProcessThrust();
    25.         ProcessRotation();
    26.     }
    27.  
    28.     void ProcessThrust() {
    29.         if (Input.GetKey(KeyCode.Space))
    30.         {
    31.             StartThrusting();
    32.         }
    33.         else
    34.         {
    35.             StopThrusting();
    36.         }
    37.     }
    38.  
    39.     void StartThrusting() {
    40.         rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
    41.         if (!audioSource.isPlaying)
    42.         {
    43.             audioSource.PlayOneShot(mainEngine);
    44.         }
    45.         if (!mainThrusterParticles.isPlaying)
    46.         {
    47.             mainThrusterParticles.Play();
    48.         }
    49.     }
    50.  
    51.     void StopThrusting() {
    52.         audioSource.Stop();
    53.         mainThrusterParticles.Stop();
    54.     }
    55.  
    56.     void ProcessRotation() {
    57.         if (Input.GetKey(KeyCode.A))
    58.         {
    59.             RotateLeft();
    60.         }
    61.         else if (Input.GetKey(KeyCode.D))
    62.         {
    63.             RotateRight();
    64.         }
    65.         else
    66.         {
    67.             StopRotating();
    68.         }
    69.     }
    70.  
    71.     void RotateLeft()
    72.     {
    73.         ApplyRotation(rotationThrust);
    74.         if (!rightThrusterParticles.isPlaying)
    75.         {
    76.             rightThrusterParticles.Play();
    77.         }
    78.     }
    79.    
    80.     void RotateRight()
    81.     {
    82.         ApplyRotation(-rotationThrust);
    83.         if (!leftThrusterParticles.isPlaying)
    84.         {
    85.             leftThrusterParticles.Play();
    86.         }
    87.     }
    88.  
    89.     void StopRotating()
    90.     {
    91.         rightThrusterParticles.Stop();
    92.         leftThrusterParticles.Stop();
    93.     }
    94.  
    95.     void ApplyRotation(float rotationThisFrame)
    96.     {
    97.         rb.freezeRotation = true; // freezing rotation so we can manually rotate
    98.         transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
    99.         rb.freezeRotation = false; // unfreezing rotation so the physics system can take over
    100.     }
    101. }
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Yeah, it's this:
    Code (CSharp):
    1. transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
    You can't mix RB and transform movement. Do everything with forces in RB.
    Also, you should be applying forces in FixedUpdate, not in Update, I think.
     
  18. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    I think the camera is unfixable for this game, I'm making this game while following a C# course and I haven't learned that much about RB movement yet, I came here because I wanted to see if there was a way I could fix the camera, the instructor for the course said I can either make it a static camera or follow the player. I think ill just deal with the slight choppiness. Sorry if i wasted your time.
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    The way to fix it is to fix the rocket movement. It's impossible to have a camera smoothly track an object that moves unevenly. Either the object will jitter in the frame, or if you turn off camera damping the object will be in a fixed position on the screen (so it won't jitter) but it will be ugly, and the background will jitter (although that's less noticeable to the viewer).
     
  20. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    Sorry, I should've phrased that better. I am still learning how to code C# and I don't know how to turn an object with code and RB. It is possible, but I don't know how to do it yet.
     
  21. ODYDEV

    ODYDEV

    Joined:
    Mar 24, 2021
    Posts:
    13
    Hello gregory, apologies for the time wasted in 2022, I was very early in the learning process of Unity and had no idea that I could use the rigidbody to set the rotation using torque.
     
    Gregoryl likes this.