Search Unity

I'd like my aim camera to always stay behind my player

Discussion in 'Cinemachine' started by steveh2112, Jan 10, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i have a pretty good 3rd person camera setup with cinemach and the orbits setting are like this
    Untitled.png

    i have aim on RMB and following a tutorial on the tube, i added an AimCamera script to the CM camera and in the code it does this
    Code (CSharp):
    1. public class CameraAim : CinemachineExtension
    2. {
    3.     public Vector3 Offset;
    4.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    5.     {
    6.         if(stage == CinemachineCore.Stage.Aim)
    7.         {
    8.             state.PositionCorrection += state.FinalOrientation * Offset;
    9.         }
    10.     }
    11. }
    Offset is .5,0,2 so it places the camera behind and a bit to the right of the player, standard stuff

    in the middle rig position, all is well but when i move up and down with the mouse, the camera lose sight of the player which i don't like

    so while the game was running i changed the top and bottom orbit to same as middle and it does what i want

    so can change that in script? or am i doing it wrong? thanks
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Yes you can change it in script. See
    CinemachineFreeLook.m_Orbits
    .
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    thanks, i kinda got it with this code
    Code (CSharp):
    1. using Cinemachine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CameraAim : CinemachineExtension
    7. {
    8.     public Vector3 Offset;
    9.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    10.     {
    11.         if (stage == CinemachineCore.Stage.Aim)
    12.         {
    13.             Debug.Log("PostPipelineStageCallback Aim");
    14.             state.PositionCorrection += state.FinalOrientation * Offset;
    15.  
    16.             Cinemachine.CinemachineFreeLook flscript = GetComponent<CinemachineFreeLook>();
    17.             CinemachineFreeLook.Orbit[] florbits = flscript.m_Orbits;
    18.  
    19.             // copy middle rig to top and bottom rigs
    20.             florbits[0].m_Radius = florbits[1].m_Radius;
    21.             florbits[2].m_Radius = florbits[1].m_Radius;
    22.         }
    23.         /*else
    24.         {
    25.             Debug.Log("PostPipelineStageCallback !Aim");
    26.             // revert to aim standard orbits
    27.             florbits[0].m_Radius = 1.75f;
    28.             florbits[2].m_Radius = 1.3f;
    29.         }*/
    30.     }
    31. }
    only problem is i don't know where to revert to the none aim settings one i exit the PostPipelineStageCallback script
    you can see i tried to do it in an else bu that doesn't work obviously

    any ideas? maybe in some other part of my main character script when i detect right mouse up
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    I don't really understand what you're trying to do. Can you describe a little more carefully the game conditions that would make you want to change the orbit dynamically, and then what conditions would make you want to change it back?

    The normal approach would be to use 2 FreeLooks, each with different orbit settings, and blend back and forth between them when the change is required.