Cinemachine: how to Prevent virtual camera movemetn on Y Axis

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

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.

5905124--630419--VcamSetup.png

1 Like

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

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).

5909693--631043--Screen Shot 2020-05-28 at 7.03.23 AM.png

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

There are several solutions, for example:

  • 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.

  • Using StateDrivenCamera:

  • When jumping, there is a vertical dead zone.

  • When not jumping, no dead zone.

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.

using UnityEngine;

namespace RunnerGame
{
    public class CamFollowMovement : MonoBehaviour
    {
        public CharacterControl control;
        public Vector3 offset;

        private void Awake()
        {
            control = GetComponentInParent<CharacterControl>();
        }

        private void Update()
        {
            transform.position = new Vector3(control.transform.position.x,
              0f, control.transform.position.z) + offset;

            if (control.transform.position.y < 0)
            {
                transform.position = new Vector3(control.transform.position.x,
              control.transform.position.y, control.transform.position.z) + offset;
            }
        }
    }
}
2 Likes

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

6001253--646145--jumpCam seetting.png
6001253--646148--posBool.png
6001253--646154--custom.png
6001253--646157--statedriven.png

1 Like

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.

sure, it is on github

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

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.

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

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.


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.

6047570--653798--JumpToDefault.jpg
6047570--653807--solution.png

1 Like