Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question First to third person camera transition problems

Discussion in 'Cinemachine' started by ItsJames3, May 2, 2024.

  1. ItsJames3

    ItsJames3

    Joined:
    Apr 23, 2023
    Posts:
    5
    I'm working on a third to first person shooter game. I'm using a free look camera for the third person mode and a virtual camera placed just in front of the players head for first person. (Would have done over the shoulder if I could but it's a uni project and a requirement is first person but anyway.)
    I've got it set up so the main camera transitions from third person to first person when you hold right mouse button which works well but when transitioning back to third person is where my problem lies.

    If I toggle the "Inherit position" option under Transitions in the third person free look camera off, the camera stays at the position that it was last at when the player was in third person and always goes back to that same position.
    If I toggle the "Inherit position" option under Transitions in the third person free look camera on, the camera will move in front of the player. I'm pretty sure this inherit position works by having the camera move to the closest position on it's orbit based off of the last active camera, and since the first person camera is just in front of the player the closest position on the free looks orbit is in front of the player, which is no good.

    Is there a way around this? Player rotation is based off of the cameras rotation so every time you unscope your player turns 180 which isn't ideal. Any help is appreciated! :)
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    It's because the POV camera's position is in front of the player, and if the center of the FreeLook orbits is the player, then it means that the closest point on the FreeLook's orbit to the POV camera is in front of the player. InheritPosition will move the camera to the closest point on the FreeLook's orbit. Here are a few options for addressing this, pick one that suits you:
    1. Move the POV camera back until it's just behind the center of the player. Adjust the near clip plane to remove unwanted artifacts
    2. Create a new empty game object child of the player and place it slightly in front of the POV camera's position. Use that game object as a LookAt and Follow target for the FreeLook.
    3. Replace the FreeLook with a simple VirtualCamera with FramingTansposer in the Body and POV in the Aim. This will create a camera with a spherical orbit which will always push the camera back when blending with InheritPosition.
     
  3. ItsJames3

    ItsJames3

    Joined:
    Apr 23, 2023
    Posts:
    5
    I was hoping you'd reply Greg, I've been a big fan of yours whenever I have to search the Unity Forums for something about cinemachine cameras.

    Number 2 worked well!

    Slight follow up question, as mentioned the players rotation replies on the main camera's rotation so as the camera moves from the two perspectives the player "wiggles" a bit awkwardly. I think to counter this I can make the players rotation based off of the current active cinemachine camera. I have a reference to both the cinemachine cameras in my script, what's the best way to tell which camera is active? I saw you replied to someone asking something similar (over 3 year ago) and you said something to do with CinemachineCore.Instance.IsLive but I don't really know what CinemachineCore is and I saw somewhere else that they removed Instance.

    Thanks!
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    Basing it off of the main camera's rotation is usually the best way. Why are you getting wiggles? Can you show a video or a gif?
    • If you have a pointer the CinemachineBrain you can use brain.ActiveVirtualCamera.State.FinalOrientation to get the active vcam's rotation.
    • You can get the brain by doing CinemachineCore.Instance.GetActiveBrain(0)
     
  5. ItsJames3

    ItsJames3

    Joined:
    Apr 23, 2023
    Posts:
    5
    Hopefully that works/shows the problem.

    Trying to use the active cinemachine camera didn't work also, if anything made it worse.

    As you can see it's a fully physics based character controller, similar to games like human fall flat and gangbeasts. We're making it multiplayer using the mirror plug in so the follow targets have to be set in the code. It might be helpful to know the third person camera now is set to follow and look at the empty child placed just in front of the characters face from your suggestion, and the first person camera is set to follow the players hips (as they're the most stable body part) and look at the top of the characters head.

    How I worded it may sound like this problem only arose after adding the empty child to look at but it has been like this the whole time I've had the two camera modes working.
     

    Attached Files:

  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    Do you get the wiggle if you disable player animation?
    What is the Binding Mode of the FreeLook?
    What is the Binding Mode of the POV cam?
     
  7. ItsJames3

    ItsJames3

    Joined:
    Apr 23, 2023
    Posts:
    5
    There is no animation! He just does that. The only thing not being controlled by an addForce() is the gun rotation/position, hand/arm position being controlled by inverse kinematics, and the feet/leg position also controlled by inverse kinematics, but I don't see them having any effect on this.

    I haven't really touched the binding modes but the FreeLook third person camera is Simple Follow With World Up, and the VirtualCamera for the first person seems to not say binding mode anywhere(?) but in Body where I would expect to find it it's 3rd Person Follow. Which does seem a bit strange for a camera intended to be first person.. but it works...

    Cameras aren't really my strong point. (which might be obvious)
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    3rdPersonFollow is kind of special. People misuse it all the time - probably because the name makes it sound like the right choice. Change it to Transposer. The choice of Binding Mode will depend on how you want it to behave:
    • If you want it to stay upright regardless of the head's movement, use Lock to Target with World Up or Lock to Target No Roll.
    • If you want it to wiggle around with the head as if hard-locked to it, use Lock To Target
    What is the LookAt target of the POV? Try using the same LookAt target as the FreeLook. What happens then?

    If you want to learn more about the correct way to use the 3rdPersonFollow component, see this excellent tutorial:

     
  9. ItsJames3

    ItsJames3

    Joined:
    Apr 23, 2023
    Posts:
    5
    So I switched the binding mode to transposer and used lock to target with world up which kind of acts the same but that's fine, more correct this way anyway.

    BUT before I came on here I had this issue and both the cameras had the same LookAt and Follow targets so (no offence) but I didn't think changing the LookAt target to the new empty game object I made earlier would fix it. However I obviously had too little faith in you as that has fixed it completely!

    I don't know how much they're paying you Greg but it needs to be more. Thank you so much!
     
    Gregoryl likes this.
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,768
    Yes, although the POV itself ignores it, the LookAt target serves as a hint for blending: the blending algorithm attempts to keep it nicely framed. Without that info, the blender can only do a slerp - which can introduce undesirable intermediate rotations.