Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Cinemachine: how to Prevent virtual camera movemetn on Y Axis

Discussion in 'Cinemachine' started by SamiRehman1997, May 27, 2020.

  1. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    hello guys, i am working on a 2.5D endless runner using cinemachine for following character, i was wondering is there's a prevent camera movement on Y axis?, I don't want it to follow player character when player is jump
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    144
    Hi,
    If you are using FramingTransposer as in our 2D example scenes (e.g. 2DConfiner scene), then you can achieve this by changing the Dead Zone Height parameter. Have a look at the attached image for an example setup.
     

    Attached Files:

  3. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    i was using transposer but doing what you suggest works, it has it's own problem tho, when there's a fall and player postion change on fall camera stuck up there which looks weird, i don't think there will be a solution for that right? so it follow whe. i have to write my own script for camera
     
    Last edited: May 28, 2020
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    144
    Yes, depending on your scenes, you may need to write a custom script.

    You may also try using Cinemachine Confiner to achieve what you'd like. You could create a rectangle that blocks the camera moving up, but does not block moving down. For example, see the attached image. Here if you jump the camera will be blocked by the Confiner. If you fall out through the hole, the camera will follow (until the camera hits the bottom of the Confiner).
     

    Attached Files:

    Last edited: May 28, 2020
  5. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    it did work my game is endless runner this solution only works when I set that confiner collider to level/platform prefab but since I have multiple platforms getting generating at runtime camera stuck on z axis, collider that is passed, expected behavior of course, I have tried attaching collider on player and camera it didn't work since camera and player both are moving and collider will move with them, yes expected behavior, confiner is good for platformers where level is already designed not randomly spawning.



    short gameplay video of my game just so you get an idea, I should have share it early sorry
     

    Attached Files:

  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    144
    There are several solutions, for example:
    1. Add an empty child gameObject to the player. This new empty gameObject will be your vcam's follow target. Add a script to this follow target that locks it to the current platform. Note, the camera will ignore all jumping, and it might be possible to jump higher than the screen.
    2. Using StateDrivenCamera:
      - When jumping, there is a vertical dead zone.
      - When not jumping, no dead zone.
     
  7. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    thank you very much, first approch it also came in my mind yesterday so i implemented that. it is working fine, not very beautiful code but it is working. i will try second solution i think that would be way better because i may want to move camera on double jump. i'll tell you if that will works. again thank you very much.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace RunnerGame
    4. {
    5.     public class CamFollowMovement : MonoBehaviour
    6.     {
    7.         public CharacterControl control;
    8.         public Vector3 offset;
    9.  
    10.         private void Awake()
    11.         {
    12.             control = GetComponentInParent<CharacterControl>();
    13.         }
    14.  
    15.         private void Update()
    16.         {
    17.             transform.position = new Vector3(control.transform.position.x,
    18.               0f, control.transform.position.z) + offset;
    19.  
    20.             if (control.transform.position.y < 0)
    21.             {
    22.                 transform.position = new Vector3(control.transform.position.x,
    23.               control.transform.position.y, control.transform.position.z) + offset;
    24.             }
    25.         }
    26.     }
    27. }
     
    gaborkb likes this.
  8. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    so I achieved what I wanted with StateDrivenCamera, since preventing movement of follow Object was messing up with physics jitter movement. I said, I will update you, so that's what i did
    1)created new virtual Cam for Jump setup settings like this.
    of course only doing that didn't work. when there was fall and character falls on platform down JumpCamera Stuck Up there.
    2) so for solving that I noticed there's a bool, that i enabled. Called Inherit Position. before Switching Camera it place jump camera to Main Camera Position.
    3)after that there was a issue with switching for smooth switching between virtual cameras, default blend is set to .3, I've multiple Vcams, so there was still some movement due to blending when cameras were blending, I needed instant switch on jump cam, but smooth with others, so i end up creating custom blend asset, and setting speed to 0 .


    I am not sure if it is the best solution since I made it with lot of trial and error & playing around with stuff, but it is working. there's still very little movement jerk like but I can't understand why it's happening but it's not that noticeable
     

    Attached Files:

    Last edited: Jun 20, 2020
    gaborkb likes this.
  9. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    144
    Your solution looks good to me.
    If you could send me your project or a project where I can reproduce the jerk, then I am going to have a look and see what might be causing the jerk.
     
  10. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    sure, it is on github

    https://github.com/SAMGameDev/Endless_2.5D_Runner
     
  11. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    144
    Your state driven camera's custom blends are not set properly. Currently, it's on cut, and maybe that's what you feel like a jerk movement. You may want to have something like an Ease in, 0.1s for Any to Any instead.
     
  12. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66

    i've tried ease in,out, linear , value to 0.2, i didn't get the any to any part.
     
  13. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
     
  14. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    144
    Double click on your custom blends and then set the behaviour. Instead of Any Camera to Any Camera, you can just set the default blend on your state driven camera.
     

    Attached Files:

  15. SamiRehman1997

    SamiRehman1997

    Joined:
    Aug 13, 2018
    Posts:
    66
    it broke everything else didn't even fix the jerk, what did it broke? smooth transition between default to slide camera (or shake cam), i only want INSTANT transition from any camera to Jump. SINCE i don't want player to feel that camera is following the player during jump. for every other camera i want smooth transition (default> slide, slide to default, slide to attack if i ever had one).

    BUT I FOUND THE PROBLEM ALSO SOLUTION.

    FIRST LET'S SEE THE PROBLEM.

    problem was in transition between jump to default camera (NOT DEFAULT TO JUMP). SEE IMAGES.

    SEE default camera postilion on y is different than jump. it is higher (OF COURSE SINCE JUMP CAM IS NOT MOVING AT ALL. WHILE DEFAULT IS FOLLOWING PLAYER ON EVERY MOVEMENT) and at that moment player is in falling state (IN Air) . so when switching
    main camera was moving up and we were seeing that weird jerk (DISTANCE BETWEEN CAMERAS IS NOT TO MUCH) that explains the jerk instant jerk.

    SOLUTION. VERY SIMPLE

    increase time alot (WHEN CAMERA SWITCH FROM JUMP TO ANY OTHER VCAM) this gives time to default camera (one that following player) to settle down on the right position (position when player is grounded and running). OR YOU CAN SAY POSTION WHERE JUMP CAMERA IS RIGHT NOW SWITCH HAPPENS WHEN BOTH CAMERAS ARE ON SAME POSTION. I HAVE PUSH THE CHANGES IN GITHUB YOU CAN CHECK IF YOU LIKE.
     

    Attached Files:

    Last edited: Jul 2, 2020 at 2:56 PM
    gaborkb likes this.
unityunity