Search Unity

Third person shooter, over the shoulder camera

Discussion in 'Cinemachine' started by RandomGeezer, Apr 6, 2018.

  1. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    Hey,
    I'm trying to configure Cinemachine for a third person shooter/over the shoulder style camera, the type of camera used in GTA 5, Metal Gear Solid 5 etc. The free look rig does most of this but I can't work out how to offset the camera so that the player is positioned off to left slightly. I tried adjusting the tracked object offset, which more or less works for the middle rig but causes to top rig to rotate at a weird angle. It also breaks collision detection when the player is close to wall, probably because the point the camera is looking at is inside the wall.

    I'm wondering if anyone knows how to set this up or has any idea where to start to get this working.

    Thanks.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    This is very easy to do with a Cinemachine Extension. Here is one that post-processes the camera position, after it has been positioned and aimed, and adds a camera-space offset to the position. Maybe that will work for you. Drop it in your project, then add it to the vcam using the Extensions dropdown.
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that offsets the camera's position
    6. /// </summary>
    7. [ExecuteInEditMode] [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class CameraOffset : CinemachineExtension
    9. {
    10.     [Tooltip("Offset the camera's position by this much (camera space)")]
    11.     public Vector3 m_Offset = Vector3.zero;
    12.  
    13.     protected override void PostPipelineStageCallback(
    14.         CinemachineVirtualCameraBase vcam,
    15.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    16.     {
    17.         // Apply after the camera has been aimed.
    18.         // To apply offset before the camera aims, change this to Body
    19.         if (stage == CinemachineCore.Stage.Aim)
    20.         {
    21.             state.PositionCorrection += state.FinalOrientation * m_Offset;
    22.         }
    23.     }
    24. }
     
  3. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    Thanks Gregoryl, the script solves the issue I was having with the offset, unfortunately it causes issue with the collider extension. I made a couple of gifs to show the problem.

    This is with the x offset set to 0.5, the camera is able to see through the wall, and my player character beomes completely obstructed before the collider does it's thing


    This is with the x offset set to 0, which of course works fine.


    Is there any way to account for the offset to prevent this happening? I've tried changing the camera radius etc in the collider extension but nothing seems to solve it.

    Thanks again for your help.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Try re-ordering the extensions, to force the collider to be applied after the offset
     
  5. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    That's how I had it set up when I created the gifs, the collider was applied last.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Try putting it in the opposite order. Does it behave the same way?
     
  7. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    Yes, it behaves the same way regardless of the order.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    The calling order is defined by the order in which the extension's Awake() is called. It doesn't make sense that changing the order doesn't affect this. How are you re-ordering? While playing? If so, try re-ordering in Edit mode, then press play.
     
  9. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    I was re-ordering in edit mode, but just to make I've just tried it again and the results are the same.

    Not sure if it's worth mentioning but the collision works fine on the opposite side, for example if the offset is 0.5 the left side doesn't clip through the wall but the right does, if it's -0.5 the right side doesn't clip but the left does.

    I'm also using this on the free look rig with the latest version of unity 2017, not sure if that affects anything.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Thanks for telling me the version, I had been basing my remarks on CM 2.1.12.
    So I checked with 2017.4 and CM 2.1.10, and indeed the extensions are applied in the order that they appear in the inspector.
    So I don't see why it shouldn't be working if you have Collider after CameraOffset. The Collider will just resolve, based on where the camera is located - including the offset. Can you send me a screenshot of your vcam inspector?
     
  11. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    Absolutely, here it is.
    1.png 2.png 3.png
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    @RandomGeezer - You're right, my bad. Changing the order won't help in this case because the Collider does its work in the Body stage, and the Offset does its work in the Aim stage, which always happens afterwards. Let me think about this a little.
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Well, the CameraOffset extension won't work for you. There's no way to get it to do its business before the Collider.

    Let's step back a little. Why not just set screen X in the 3 composers to be a little left of the middle - say 0.35 instead of 0.5?
     
  14. RandomGeezer

    RandomGeezer

    Joined:
    Apr 6, 2018
    Posts:
    7
    I played around a bit with the screen X values on the composers, but because it rotates rather then simply moving the camera along the X axis it doesn't give the desired result. I can explain this better tomorrow with screenshots if you need it, it's a little late here in the uk for me to do it now.

    The camera offset extension did exactly what I needed, but of course I need collision as well, so it's a shame it won't work.

    Thanks again for helping with this.
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Don't give up yet on the screen offset. If you combine it with just the right heading bias on the FreeLook, it will give the same effect as the camera offset - and it will work with collisions
     
  16. BrandStone

    BrandStone

    Joined:
    Jul 21, 2014
    Posts:
    79
    With some experimentation I found out that Camera Collider works if you set Z parameter to 0 in the Camera Offset Extension.

    If you want to adjust the camera distance to target you can do it in the Cinemachine Free Look component / Orbits / TopRig, MiddleRig and BottomRig radiuses.

    Edit: nevermind, having the same issue as @RandomGeezer. Any update with a more robust way to have the cake and eat it too? :)
     
    Last edited: Aug 26, 2018
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    I think screen offset + heading bias, carefully tuned, is the way to go.
     
  18. Invector

    Invector

    Joined:
    Jun 23, 2015
    Posts:
    966
    I'm also stuck trying to figure it out a way to use the screen offset extension and collider, but it's definitely not working as expected:confused: - I think it's really strange that such a powerful tool like Cinemachine with tons of advanced options out of the box, but doesn't have a simple offset which is the first thing you think of when creating a 3rd person shooter or any over the shoulder kind of game.
     
    pau1o-hs likes this.
  19. andrejpetelin

    andrejpetelin

    Joined:
    Oct 14, 2017
    Posts:
    31
    I'm a bit late to the party here, but thanks for this... and even more so, thanks for the now built in Cinemachine Camera Offset extension. :)