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

How to enable input on a different virtual camera

Discussion in 'Cinemachine' started by siennakei, May 10, 2022.

  1. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    I'm pretty new to unity so apologies in advance if my logic is a bit off. Basically I have a section in my game where i'd like the player to transition to, on the click of a button. I'd like the camera to show this transition too. To explain what i mean a bit better, it's kind of like getting inside a locker. So the player is outside the locker, you click the locker, and the player moves to inside the locker, with the camera tracking POV.

    I've tried all sorts without much luck, but what I've found looks best is creating a virtual camera in the position where I want the player to be, and then setting the priority of the camera, when the button is clicked. It's a really nice and smooth transition so that's great. The issue I'm having though, is that player input isn't enabled on the virtual camera so i can't move around or use any inputs. I've tried setting the 'follow' field to my player, but then the inputs are all skewed when looking around. I'm guessing this is because the player itself hasn't actually moved.

    I have a first person player character.

    Is what I'm trying to achieve possible with virtual cameras? Can i somehow set another player root to the new virtual camera so the inputs and camera rotation are centred to the new virtual camera? Or maybe there's a different way of doing this entirely.

    Thanks for any help given, i really appreciate it.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you want the player to get inside the locker, then the player should get inside the locker. If you try to fake it by just putting the camera inside the locker, then of course all your movement code will be wrong.

    You could do it by making a separate player inside the locker, with its own POV vcam. Then, you would have to disable the first player and enable the new one, transferring all your movement and player logic to it. If you do that, then the vcam blend will do the job for you.
     
  3. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    Yes, i thought I was doing it wrong :(
    Thanks for your suggestion, making a whole new player character seems a lot of work, just to move the player from one point to another though. In unreal it was just a matter of changing the camera values, but it seems this isnt very easy in unity. I have also tried making a simple animation for this transition, but when attaching the animator to the player, it causes a whole load more issues.

    Is creating a completely different player really the best way to achieve this?
    I should mention, I'm building in WEBGL for browser so i'm trying to save on memory as much as i can.
    Thanks again for your reply
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Probably not.

    I'm going to need some details about how you've set up your player. Can you show me your hierarchy and inspectors for:
    - player
    - main camera
    - player vcam
    - locker vcam
     
  5. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    Sure, here they are
     

    Attached Files:

  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Thanks.

    If I understand correctly, the user controls the player capsule to move the player around. The player vcam is a slave to that player capsule, and positions itself in relation to it. The users don't control the camera, they control the player position (and the player camera root, for looking around).

    The main camera should not be a child of the player. It should be an independent game object. The CinemachineBrain on the main camera has the job of positioning itself in response to the current active vcam. It gets puppeteered, so it is counterproductive to also throw parenting into the mix. Take it out.

    So now you want to move the player into the locker, and still retain user control of the player (i.e. not just a camera trick). Now I need to understand: why are you trying to do it with a camera trick, instead of actually moving the player into the locker?
     
    Last edited: May 10, 2022
  7. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    Ah ok, thanks for that tip, I'll remove the parenting for main camera then :)

    So i tried to move the player with an animation, (when click the locker the animation plays) but attaching the animator to the player caused some issues where the player input again was being over-ridden by the 'idle' state. Another reason i tried with the virtual camera was becsaue from whatever position the player was, the camera moved very smoothly and nicely into place.

    I also tried moving the player position with '.transform ' but this just moved the player from one point to the next, without the camera transitioning to show the movement, it was more just teleporting the player.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Yes, if you set the position in the transform, it will teleport. What you need to do is to programmatically move the player position to the desired spot, gradually over the course of a few frames. The camera will naturally follow by default, and all the player controls will continue to work (you might want to disable player position control while the programmed move is happening).

    There are a couple of ways to move the player:
    1. By script. Write a custom behaviour that changes the position using SmoothDamp.
    2. Using an animator. You say you had some issues with this. Maybe you didn't set it up correctly. Maybe you didn't turn off the animator when the movement was completed. I can't really help you here (I'm more of a script guy), but you could ask over on the animation forum.
     
  9. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    OK, I'll look into programmatically moving the player. Thank you. I'm not good with scripting but i'll give it a shot. With this method, would i have to move the player into the position of where the movement begins for it to work correcting? Like, does the player have to start in the exact position where the movement begins form? sorry if im not explaining myself properly.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    With code, you can just move the player from wherever it happens to be to the desired spot. One naive and easy way is to move it half the remaining distance each frame. Of course, it will never exactly reach the target position, but your code can stop moving it when it's "close enough".

    Less naively, you can use SmoothDamp.
    https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html
     
  11. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    Thank you very much, i really appreciate this info. I'll try smoothDamp and see how I go.