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.
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.
using UnityEngine;
using Cinemachine;
/// <summary>
/// An add-on module for Cinemachine Virtual Camera that offsets the camera's position
/// </summary>
[ExecuteInEditMode] [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
public class CameraOffset : CinemachineExtension
{
[Tooltip("Offset the camera's position by this much (camera space)")]
public Vector3 m_Offset = Vector3.zero;
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
{
// Apply after the camera has been aimed.
// To apply offset before the camera aims, change this to Body
if (stage == CinemachineCore.Stage.Aim)
{
state.PositionCorrection += state.FinalOrientation * m_Offset;
}
}
}
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
MadFatChanticleer
This is with the x offset set to 0, which of course works fine.
RedImaginativeCrocodile
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.
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.
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.
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?
@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.
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.
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
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?
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:face_with_spiral_eyes: - 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.