Search Unity

Set vcam zoom (orto size) based on player speed

Discussion in 'Cinemachine' started by nicmarxp, Sep 12, 2019.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    I'm using a follow cinemachine camera, and I want the camera to zoom out when the player moves fast, up to a certain limit, and zoom in when they player slows down.

    Is there any premade functions in cinemachine to achieve this, or should I write a custom script and connect my player speed to the orthographic size?

    Thanks for any ideas!
     
    moosesnWoop likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    There a re a couple of approaches to this, but both require a little scripting.

    Either do as you suggest (making sure to adjust the ortho size of the vcam, not the main camera), or you can try this other idea:
    1. Create an invisible game object, with a custom script to place it always some distance in front of the player, with the distance scaled by the velocity
    2. Add that object and the player to a CinemachineTargetGroup
    3. Use that group as the vcam target
    4. Set vcam's FramingTransposer settings to frame the group the way you like it
     
  3. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Ah, thanks Gregoryl, that was an interesting approach, as it seems to let me adjust the framingtransposer instead of having variables in my code. I will definitely try it :)
     
  4. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hi again @Gregoryl! I've now tried this, and got a decent result, but I still have some "jerkiness" when the framing transposer zooms in and out.

    I made a short video that shows what I mean.


    The orange dot is the "invisible game object" that is lerping in the direction the player is going. On the left you see when the orange frame expands (Framing transposer limits?), the vcam zooms out too quickly causing this jerkiness.

    I tried both 0 and 20 on the X/Y damping, and I got different results how the camera moves while the ship is in the rectangle, but not how it changes the orthographic size.

    Can you suggest any values I should tweak to fix this?

    Also it seems to do some small jerks back and forth when it's on max speed, but I'm not sure if it's due to me playing in the editor, or anything else.

    Thank you in advance!
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    The Z damping controls the damping on the ortho size change
     
  6. Visionalmind

    Visionalmind

    Joined:
    Mar 22, 2019
    Posts:
    3
    Heej nicmarxp, I want to build something similar like your game. So I try to find a similar solution for the camera. But I'm new to Unity, and still learning programming. So my question to you is, how did you achieve that invisible game object to lerp like that? Is there a tutorial for this? In what direction should I look? Thanks for reading, have a great day you all.
     
  7. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hehe, better late than never. This is what I created. I just ended up stop using it due to the camera jerking at higher speeds, so right now I'm back to just following the player, without changing the zoom, but I'm considering attaching a script to set the ortho based on velocity.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace BlueGoo.Space {
    5.     public class ForwardCameraTarget : MonoBehaviour {
    6.  
    7.         public Transform source;
    8.         private ShipController2 shipController;
    9.        
    10.         [SerializeField]
    11.         private float distanceScale;
    12.  
    13.         [SerializeField]
    14.         private float maxDistance;
    15.  
    16.         [SerializeField]
    17.         private float minDistance;
    18.  
    19.         [SerializeField]
    20.         private float lerpSpeed = 1;
    21.        
    22.        
    23.         private void Start() {
    24.             // TODO: Assign player on start / Switch
    25.             AssignPlayer();
    26.         }
    27.  
    28.         public void AssignPlayer() {
    29.             shipController = source.gameObject.GetComponent<ShipController2>();
    30.         }
    31.  
    32.         private void Update() {
    33.             var velocity = shipController.rb.velocity;
    34.             var motionVector = velocity.normalized;
    35.             var motionMagnitude = velocity.magnitude;
    36.             var targetPosition = source.position + (Vector3)(motionVector *
    37.                                                              (Mathf.Max(minDistance,
    38.                                                              (Mathf.Min(maxDistance,motionMagnitude) * distanceScale)
    39.                                                              )));
    40.            
    41.             transform.position = Vector3.Lerp(transform.position, targetPosition, lerpSpeed * Time.fixedDeltaTime);
    42.  
    43.         }
    44.     }
    45. }
     
    andreyshade and ferverence like this.
  8. moosesnWoop

    moosesnWoop

    Joined:
    Sep 12, 2019
    Posts:
    12

    Have you tried running it in a fixed or late update? The transform.position = Vector3.Lerp(transform.position, targetPosition, lerpSpeed * Time.fixedDeltaTime);

    That may stop the jerkiness of the camera movement?

    I'm trying to get the same effect for my game at the moment and looking at ideas.
     
  9. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks, I have paused that for a little while, but I will get back to it soon and see if I did test that or not.. :) Let me know your results!