Search Unity

Cinemachine FreeLook camera and New Input System?

Discussion in 'Input System' started by MlleBun, Nov 7, 2019.

  1. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hello,
    Are those two compatible or will they be in future release? For now, I'm using Old input system for Input Axis Name parameter in the CM Free Look cam and the new system in my character controller. But I was wondering if CM will support the new input system later or vice-versa.
    Thanks.
     
  2. Gandarufu

    Gandarufu

    Joined:
    Nov 10, 2014
    Posts:
    22
    This worked for me:
     
    TJHeuvel-net, maryush28, SN8K and 4 others like this.
  3. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    I bet you did :confused: I'm having headaches too. Many thanks for your help, I'm going to test it tonight!
     
  4. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hello @Gandarufu, again thanks for the help. Just to let you and others know some points.

    1. As Cinemachine still needs the old input system, don't forget to check if you have "Mouse X" and "Mouse Y" inputs in the Input list ; or if you choose to have custom names (in my case it's "CamControlX" and "CamControlY"), don't forget to create them in the old Input Manager list as you'll get errors. I'm hoping that won't be mandatory later.

    System.ArgumentException: Input Axis CamControlX is not setup.
    To change the input settings use: Edit -> Settings -> Input a
    t (wrapper managed-to-native) UnityEngine.Input.GetAxis(string)

    2. Also, I modified a little bit your code, LookDelta not being updated when moving my gamepad rightstick.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class InputCameraController : MonoBehaviour
    5. {
    6.     private PrototypeInputSystem inputSystem;
    7.     private Vector2 LookCamera; // your LookDelta
    8.     public float deadZoneX = 0.2f;
    9.  
    10.     private void Awake()
    11.     {
    12.         inputSystem = new PrototypeInputSystem();
    13.  
    14.         inputSystem.PlayerGameplay.CameraControl.performed += ctx => LookCamera = ctx.ReadValue<Vector2>().normalized;
    15.         inputSystem.PlayerGameplay.CameraControl.performed += ctx => GetInput();
    16.     }
    17.  
    18.     private void GetInput()
    19.     {
    20.         CinemachineCore.GetInputAxis = GetAxisCustom;
    21.     }
    22.  
    23.     public float GetAxisCustom(string axisName)
    24.     {
    25.         // LookCamera.Normalize();
    26.  
    27.         if (axisName == "CamControlX")
    28.         {
    29.           if (LookCamera.x > deadZoneX || LookCamera.x < -deadZoneX) // To stabilise Cam and prevent it from rotating when LookCamera.x value is between deadZoneX and - deadZoneX
    30.            {
    31.              return LookCamera.x;
    32.            }
    33.         }
    34.  
    35.         else if (axisName == "CamControlY")
    36.         {
    37.             return LookCamera.y;
    38.         }
    39.  
    40.         return 0;
    41.     }
    42.  
    43.     private void OnEnable()
    44.     {
    45.         inputSystem.PlayerGameplay.Enable();
    46.     }
    47.  
    48.     private void OnDisable()
    49.     {
    50.         inputSystem.PlayerGameplay.Disable();
    51.     }
    52. }
    53.  
    And it's working beautifully!
     
    Last edited: Mar 9, 2020
  5. northman

    northman

    Joined:
    Feb 28, 2008
    Posts:
    144

    Hi @MlleBun , Today I just learned new Input System and Cinemachine. I try to make the Action in Editor dialog, but I got issue when it ran.
    Please help me to check my setup and figerout the problem.

    Best regards,
     
  6. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi @northman, are you having errors or is the script working but you are not having the desired behaviour on your mouse control?
     
  7. Worstharbor

    Worstharbor

    Joined:
    Jan 29, 2020
    Posts:
    3
    Ok so I did the thing all I'm trying to do is set up the mouse to be used by CM

    It's not exactly moving but there are no errors in the code. I'm not sure if I should add it as a component to the camera? Should I use Unity Engine.Inputcontroller? I am at a stalemate.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4.  
    5. public class FreeLookUserInput : MonoBehaviour
    6. {
    7.     private PlayerControls playerControls;
    8.     private Vector2 lookCamera;
    9.     public float deadZoneX = 0.2f;
    10.     // Start is called before the first frame update
    11.     private void Awake()
    12.     {
    13.         playerControls = new PlayerControls();
    14.  
    15.         playerControls.Player.Look.performed += ctx => lookCamera = ctx.ReadValue<Vector2>().normalized;
    16.         playerControls.Player.Look.performed += ctx => GetInput();
    17.     }
    18.  
    19.     private void GetInput()
    20.     {
    21.         CinemachineCore.GetInputAxis = GetAxisCustom;
    22.     }
    23.  
    24.     public float GetAxisCustom(string axisName)
    25.     {
    26.         //LookCamer.Normalized
    27.         if(axisName == "Mouse X")
    28.         {
    29.             if(lookCamera.x > deadZoneX || lookCamera.x < -deadZoneX)
    30.             {
    31.                 return lookCamera.x;
    32.             }
    33.         }
    34.         else if (axisName == "Mouse Y")
    35.         {
    36.             return lookCamera.y;
    37.         }
    38.         return 0;
    39.     }
    40.     private void OnEnable()
    41.     {
    42.         playerControls.Player.Enable();
    43.     }
    44.     private void OnDisable()
    45.     {
    46.         playerControls.Player.Disable();
    47.     }
    48. }
     

    Attached Files:

  8. nettamenteBoss

    nettamenteBoss

    Joined:
    Jun 16, 2013
    Posts:
    2
    Hi,
    I've managed the problem in this way:


    Code (CSharp):
    1. public class SwitchCamera : MonoBehaviour
    2. {
    3.  
    4.     public GameObject cinemachine;
    5.  
    6.     private CinemachineFreeLook freeLook;
    7.  
    8.     void Start()
    9.     {
    10.         freeLook = cinemachine.GetComponent<CinemachineFreeLook>();
    11.  
    12.  
    13.     }
    14.  
    15.     public void Look(InputAction.CallbackContext context)
    16.     {
    17.  
    18.  
    19.         freeLook.m_XAxis.m_InputAxisValue = context.ReadValue<Vector2>().x;
    20.         freeLook.m_YAxis.m_InputAxisValue = context.ReadValue<Vector2>().y;
    21.  
    22.     }
    23.  
    24.  
    25. }
    - Assigned my cinemachine FreeLook Camera to "cinemachine" in the Editor .

    - Left blank the fields "Input Axis Name" both in Axis Control > Y Axis and Axis Control> X Axis

    - Assigned the SwitchCamera.Look function to Events> Player > Look (Callback Context) in the Player Input Component, according to https://docs.unity3d.com/Packages/c...ting-input-indirectly-through-an-input-action
     
  9. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    @Gregoryl Any authoritative advice on this? And are there any plans to free Cinemachine from the clutches of the old input system?
     
    MlleBun likes this.
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @transat We are working on better support for the New Input System
     
  11. Beshop_

    Beshop_

    Joined:
    Feb 5, 2020
    Posts:
    1
    Hello, I'm working on my project and I have this problem, but I tried this solution and it's fine, I haven't error but now when I move my mouse, the freelook cam turn but don't stop, even when I release my mouse. Someone has met this problem ?
     
  12. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi
    Hi @Beshop_ , could you post a screenshot of your Input actions to see your setup? Thanks ;)
     
  13. JadeTheurgist

    JadeTheurgist

    Joined:
    Apr 11, 2015
    Posts:
    1
    Hey @Beshop_ , I think I was having the same problem you were, where the camera would continue rotating in a direction even after you stopped giving input. I managed to find a solution. Along side where the inputs are assigned in the code's Awake function add this:

    Code (CSharp):
    1. playerControls.Player.Look.canceled += ctx => LookCamera = Vector2.zero;
    This makes it so when you stop giving input, the LookCamera Vector2 will reset its value and stop moving rather than retain the last value it had and keep moving the camera.

    Hope this helps!
     
  14. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi everyone, got the same issue with the mouse, but I could solve it just by changing the binding with no change in the script shared above :

    upload_2020-5-30_11-6-36.png

    Hope it solves your problem!
     
  15. smontambault64

    smontambault64

    Joined:
    Sep 7, 2018
    Posts:
    1
    afshin_a_1, Asprewga and Savdog like this.
  16. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    trying to follow a tutorial on youtube about creating a third person controller, two hours later I'm trying to find a fix for cinemachine and input package. unity learning workflow never let me down.
     
    GhoulDrago and michaelgrilo like this.
  17. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    Agree re the Unity learning workflow. But FYI, Cinemachine 2.6 now has a component you can attach to your virtual cameras to fix the issue. If you spend another hour on the Unity blog and forum you will find info about it. Good luck!

    On a less cynical note, documentation aside, the Cinemachine team is actually one of the best at Unity. They do quality work and are quite responsive on the forum so no doubt you will get the answers you are looking for.
     
    afshin_a_1 and Hexalted like this.
  18. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    What about helping a fellow forum member and just tell me how to do this instead
     
  19. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    From memory you add a component called input axis provider (or something like that) to your virtual camera. I'm not in front of my project right now so can't be more specific.
     
    camsha likes this.
  20. camsha

    camsha

    Joined:
    Nov 18, 2013
    Posts:
    11
    Add a CinemachineInputProvider component.
     
    peonso and Hexalted like this.
  21. Ujjwal_Rajput

    Ujjwal_Rajput

    Joined:
    Jun 4, 2020
    Posts:
    5
    using the script given above unity giving error that you are using new new input system, so i made this script it is working ok with mouse position in key binding we can use even delta but i don't know how to make came look smoot.
    if anyone know how to solve it please improve this script


    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2. using UnityEngine;
    3. using Cinemachine;
    4.  
    5. public class CameraLook : MonoBehaviour
    6. {
    7.     PlayerInput playerInput;
    8.     CinemachineFreeLook cinemachineFreeLook;
    9.  
    10.     private void Awake() {
    11.         playerInput = new PlayerInput();  // script created by new input system
    12.         cinemachineFreeLook = GetComponent<CinemachineFreeLook>();
    13.         playerInput.GroundInput.Camera.performed += CameraDelta;
    14.     }
    15.  
    16.     void CameraDelta(InputAction.CallbackContext context) {
    17.         Vector2 LookDelta = context.ReadValue<Vector2>();
    18.         cinemachineFreeLook.m_XAxis.Value = LookDelta.x;
    19.         LookDelta.Normalize();
    20.         cinemachineFreeLook.m_YAxis.Value = LookDelta.y;
    21.     }
    22.  
    23.  
    24.  
    25.  
    26.     private void OnEnable() => playerInput.GroundInput.Enable();
    27.  
    28.     private void OnDisable() => playerInput.GroundInput.Disable();
    29. }
    30.  
     
  22. michaelgrilo

    michaelgrilo

    Joined:
    Oct 15, 2018
    Posts:
    21
  23. sysfer

    sysfer

    Joined:
    Jun 8, 2018
    Posts:
    1
    using UnityEngine;
    using Cinemachine;
    using UnityEngine.InputSystem;


    [RequireComponent(typeof(CinemachineFreeLook))]
    public class CameraLook : MonoBehaviour{

    private CinemachineFreeLook cinemachine;
    public PlayerInputSystem GetplayerInput;
    [SerializeField]
    public float lookSpeed=1;

    private void Awake() {
    GetplayerInput = new PlayerInputSystem(); // script created by new input system
    cinemachine = GetComponent<CinemachineFreeLook>();
    }

    void Update(){
    Vector2 delta = GetplayerInput.PlayerMain.Look.ReadValue<Vector2>();
    cinemachine.m_XAxis.Value += delta.x * lookSpeed * 200 * Time.deltaTime;
    cinemachine.m_YAxis.Value += delta.y * lookSpeed * Time.deltaTime;
    }

    private void OnEnable(){
    GetplayerInput.Enable();
    }

    private void OnDisable(){
    GetplayerInput.Disable();
    }


    }
     
  24. SergioR

    SergioR

    Joined:
    May 6, 2020
    Posts:
    1
    Im having the same issue.. I am using the new input system and when I tried to add a Cinemachine free look camera this error shows up:

    InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.
    Cinemachine.AxisState.Update (System.Single deltaTime) (at Library/PackageCache/com.unity.cinemachine@2.6.3/Runtime/Core/AxisState.cs:206)

    Tried to attach the script mentioned above to the main camera and also to the cinemachine camera but still the same error.

    Any idea why this happens?
     
  25. ssedlmayr

    ssedlmayr

    Joined:
    Feb 28, 2013
    Posts:
    36
    If people are still experiencing this issue as I was as of April 2021, here is what worked for me. This thread was a huge help, as were some of the links from this page, but ultimately none of the solutions here worked for me. I tried both PlayerInput and CinemachineInputProvider solutions as people mentioned above and neither of those things worked. I also tried subclassing CinemachineInputProvider and overriding GetAxisValue because their current implementation reads Vector2 values from the axes, which is deprecated. The Unity InputSystem now provides float values for up, down, left and right rather than a Vector2 with values between -1.0f and 1.0f, so it was throwing errors. This effort failed, however, because the method was never being called. I could not find a way to actually set the input provider anywhere. This is what that code looked like:

    Code (CSharp):
    1. public class HvLmCinemachineInputProvider : CinemachineInputProvider {
    2.     /// <summary>
    3.     /// Implementation of AxisState.IInputAxisProvider.GetAxisValue().
    4.     /// Axis index ranges from 0...2 for X, Y, and Z.
    5.     /// Reads the action associated with the axis.
    6.     /// </summary>
    7.     /// <param name="axis"></param>
    8.     /// <returns>The current axis value</returns>
    9.     public override float GetAxisValue(int axis) {
    10.         if (enabled) {
    11.             var action = ResolveForPlayer(axis, axis == 2 ? ZAxis : XYAxis);
    12.              
    13.             if (action != null) {
    14.                 switch (axis) {
    15.                     case 0: return action.ReadValue<float>();
    16.                     case 1: return action.ReadValue<float>();
    17.                     case 2: return action.ReadValue<float>();
    18.                 }
    19.             }
    20.         }
    21.  
    22.         return 0;
    23.     }
    24. }
    25.  
    However, this exercise made me realize that Cinemachine does in fact have InputSystem support, because it was throwing errors in a method implementing such support, the Cinemachine code throwing the errors, upon inspection, contains pre-compiler directives regarding the InputSystem, and also the above statement from Gregoryl 4 years ago that they would be adding such support.

    After following the excellent Brackeys Youtube tutorial on writing a 3rd-person character controller with a Unity CharacterController and a CinemachineFreeLook camera, another blog post, mentioned above, reading other references about configuring the CinemachineFreeLook, and later the Brackeys Youtube tutorial about writing a 1st person character controller, I have iterated to a fairly solid solution. Both of the Brackeys tutorials and the blog post mentioned were written with the old InputManager in mind, so I had to re-write/re-configure the portions regarding the actual input. The first step is familiarizing yourself with how the new InputSystem works. After that, the key thing is removing the string text in the Input Axis Value fields in the CinemachineFreeLook inspector in the editor. Brackeys mentions this in his 3POV tutorial, so briefly and quickly I almost missed it. This small detail is what triggers the Cinemachine system to support the InputSystem fully, without throwing any exceptions, as of Unity 2021.1.3f1. It is not well documented AFAIK, and is easy to miss. After that, it worked fine. You don't need to do any hacks of the Cinemachine system, messing around with the Cinemachine core, subclassing Cinemachine classes, adding PlayerInput, adding a CinemachineInputProvider, or anything like that; just emptying out those two strings. You then need to configure an InputActions instance with action maps, add bindings to the action maps, enable them, etc. (see above comment about understanding how the InputSystem works first).

    The below controller is what I eventually ended up with for the prototype I am building. Note that it is a work in progress and contains a lot of detail you might not need, not to mentioned static compiler references in inspector properties and other things you would need to add and configure to/in your scene graph for it to work properly, not to mention tuning of certain fields based upon the scale of your character assets. It also contains references to code in a personal assembly of reusable code. But I'm posting the whole thing in case it helps someone with the broader goal of having a working 3POV controller using the InputSystem, a CharacterController and a CinemachineFreeLook (in this case for a Gamepad, but it will work with any inputs you map). It is again based on Brackeys' two Youtube tutorials about 1POV and 3POV controllers, and a blog post, synthesized together, modified for my own needs and optimized (although some optimizations still have to be undertaken, mainly moving as much logic as possible out from being invoked from Update). The links to everything are included in the code, and also provided in linkable form after the code window:

    Code (CSharp):
    1. using Cinemachine;
    2.  
    3. using dk.Tools.Debug;
    4.  
    5. using HvLm.Proto.IO;
    6. using HvLm.Proto.Mixins;
    7. using HvLm.Proto.Scene;
    8.  
    9. using System.Collections;
    10.  
    11. using UnityEngine;
    12. using UnityEngine.InputSystem;
    13.  
    14. namespace HvLm.Proto.Character {
    15.     /// <summary>
    16.     /// Character control solution using Unity's built-in CharacterController component and CinemachineFreeLook virtual camera component.
    17.     /// I referenced this Brackeys video: https://www.youtube.com/watch?v=4HpC--2iowE; and this blog post:
    18.     /// https://smontambault.medium.com/unity-cinemachine-with-new-input-system-b608e13997c7. I heavily modified both for optimization
    19.     /// and to update them for the new Unity.InputSystem. For movement, I also referenced (but did not necessarily use):
    20.     ///
    21.     /// https://www.youtube.com/watch?v=98MBwtZU2kk
    22.     /// https://docs.unity3d.com/ScriptReference/CharacterController.html
    23.     /// https://docs.unity3d.com/Manual/class-CharacterController.html
    24.     /// https://forum.unity.com/threads/cinemachine-freelook-camera-and-new-input-system.773657/
    25.     /// https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html?_ga=2.216236246.136778867.1618248986-1943268407.1608668420#getting-input-indirectly-through-an-input-action
    26.     /// https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.Controls.StickControl.html
    27.     ///
    28.     /// For rigid body physics I have looked at the following but will probably also reference a Brackeys video:
    29.     ///
    30.     /// https://www.reddit.com/r/Unity3D/comments/19k1xh/applying_force_to_the_first_person_controller/
    31.     /// https://answers.unity.com/questions/374577/rigidbody-vs-character-controller.html
    32.     /// http://wiki.unity3d.com/index.php/RigidbodyFPSWalker
    33.     /// https://forum.unity.com/threads/apply-an-addforce-type-of-effect-to-a-kinematic-rigidbody2d.370091/
    34.     /// https://answers.unity.com/questions/255689/rigidbody-object-falling-through-floor.html
    35.     ///
    36.     /// Here is the Brackeys video I intend to reference: https://www.youtube.com/watch?v=_QajrabyTJc
    37.     ///
    38.     /// I also referenced:
    39.     ///
    40.     /// https://gamedev.stackexchange.com/questions/104825/character-controller-passes-through-mesh-collider
    41.     /// </summary>
    42.     public class Player3POVXBoxMotionController : BaseMotionController<MainScene,HiveliminalPrototypePlayerInputActions>, IInputSystemDetector {
    43.         //private const float _HANDLE_RIGHT_STICK_INTERVAL = 0.0333f;
    44.         //private const float _HANDLE_RIGHT_STICK_INTERVAL = 0.0165f;
    45.         //private const float _HANDLE_RIGHT_STICK_INTERVAL = 0.0111f;
    46.         private const float _HANDLE_RIGHT_STICK_INTERVAL = 0.00825f;
    47.  
    48.         #region Inspector properties
    49.  
    50.         #region Static compiler heap references
    51.  
    52.         [SerializeField]
    53.         protected GameObject _avatar = null;
    54.  
    55.         [SerializeField]
    56.         protected Animator _animator = null;
    57.  
    58.         [SerializeField]
    59.         protected RuntimeAnimatorController _rac = null;
    60.  
    61.         [SerializeField]
    62.         protected CharacterController _controller = null;
    63.  
    64.         [SerializeField]
    65.         protected CinemachineFreeLook _freeLookComponent;
    66.  
    67.         [SerializeField]
    68.         protected Transform _camera = null;
    69.  
    70.         #endregion
    71.  
    72.         #region Camera and movement
    73.  
    74.         [SerializeField]
    75.         protected float _speed = 0.0f;
    76.  
    77.         [SerializeField]
    78.         protected float _turnSmoothTime = 0.1f;
    79.  
    80.         [Range(0f, 10f)]
    81.         [SerializeField]
    82.         protected float _xLookSpeed = 1f;
    83.  
    84.         [Range(0f, 10f)]
    85.         [SerializeField]
    86.         protected float _yLookSpeed = 1f;
    87.  
    88.         [SerializeField]
    89.         protected bool _invertY = false;
    90.  
    91.         #endregion
    92.  
    93.         #region Ground check
    94.  
    95.         [SerializeField]
    96.         protected Transform _groundCheck = null;
    97.  
    98.         [SerializeField]
    99.         protected float _groundDistance = 0.4f;
    100.  
    101.         [SerializeField]
    102.         protected LayerMask _groundMask = default(LayerMask);
    103.  
    104.         #endregion
    105.  
    106.         #endregion
    107.  
    108.         #region Movement fields
    109.  
    110.         private bool _isJumping = false;
    111.         private float _jumpHeight = 0.00001f;
    112.  
    113.         private float _leftStickX = 0f;
    114.         private float _leftStickZ = 0f;
    115.  
    116.         #endregion
    117.  
    118.         #region Free look camera fields
    119.  
    120.         private bool _handlingRightStick = false;
    121.         private float _invert = 1.0f;
    122.         private float _rightStickX = 0f;
    123.         private float _rightStickY = 0f;
    124.         private float _rightstickDeadzoneX = 0.003f;
    125.         private float _rightstickDeadzoneY = 0.003f;
    126.         private float _turnSmoothVelocity = 0.0f;
    127.         private Hashtable _startRightStickArgs = null;
    128.         private Hashtable _continueRightStickArgs = null;
    129.  
    130.         #endregion
    131.  
    132.         #region Gravity and ground check
    133.  
    134.         private bool _isGrounded = true;
    135.         private float _gravity = -9.81f;
    136.         Vector3 _yVelocity = Vector3.zero;
    137.  
    138.         #endregion
    139.  
    140.         private Vector3 _originalAvatarPosition = Vector3.zero;
    141.  
    142.         #region Monobehaviours
    143.  
    144.         /// <summary>
    145.         /// Starts this instance.
    146.         /// </summary>
    147.         override protected void Start() {
    148.             base.Start();
    149.  
    150.             Cursor.lockState = CursorLockMode.Locked;
    151.  
    152.             _invert = (_invertY == true) ? -1.0f : 1.0f;
    153.             _originalAvatarPosition = _avatar.transform.localPosition;
    154.         }
    155.  
    156.         /// <summary>
    157.         /// Updates this instance.
    158.         /// </summary>
    159.         override protected void Update() {
    160.             base.Update();
    161.  
    162.             CheckGround();
    163.             CheckJump();
    164.             Move();
    165.             ProcessGravity();
    166.         }
    167.  
    168.         /// <summary>
    169.         /// Handles fixed update.
    170.         /// </summary>
    171.         void FixedUpdate() {
    172.             _isGrounded = Physics.CheckSphere(_groundCheck.position, _groundDistance, _groundMask);
    173.  
    174.             _leftStickX = Gamepad.current.leftStick.x.ReadValue();
    175.             _leftStickZ = Gamepad.current.leftStick.y.ReadValue();
    176.          
    177.             _rightStickX = Gamepad.current.rightStick.x.ReadValue();
    178.             _rightStickY = Gamepad.current.rightStick.y.ReadValue();
    179.         }
    180.  
    181.         #endregion
    182.  
    183.         /// <summary>
    184.         /// Ground check.
    185.         /// </summary>
    186.         [ToDo("Optimize the ground check code out of the Update loop like with the free look code.", false, 101)]
    187.         protected void CheckGround() {
    188.             if (_isGrounded &&
    189.                 !_isJumping &&
    190.                 _yVelocity.y < 0) {
    191.                 _yVelocity.y = -1f;
    192.             }
    193.         }
    194.  
    195.         /// <summary>
    196.         /// Jump check.
    197.         /// </summary>
    198.         [ToDo("Optimize the jump check code out of the Update loop like with the free look code.", false, 102)]
    199.         protected void CheckJump() {
    200.             if (_isJumping && _isGrounded) {
    201.                 _yVelocity.y = Mathf.Sqrt(_jumpHeight * -2f * _gravity);
    202.             } else if (_isJumping && !_isGrounded) {
    203.                 _isJumping = false;
    204.             }
    205.         }
    206.  
    207.         /// <summary>
    208.         /// Movement.
    209.         /// </summary>
    210.         [ToDo("Optimize the move out of the Update loop like with the free look code.", false, 100)]
    211.         protected void Move() {
    212.             Vector3 direction = new Vector3(_leftStickX, 0f, _leftStickZ).normalized;
    213.          
    214.             if (direction.magnitude >= 0.01f) {
    215.                 if (_isGrounded) {
    216.                     _animator.Play(_rac.animationClips[1].name);
    217.                 } else {
    218.                     _animator.Play(_rac.animationClips[0].name);
    219.                 }
    220.  
    221.                 float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + _camera.eulerAngles.y;
    222.                 float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref _turnSmoothVelocity, _turnSmoothTime);
    223.                 transform.rotation = Quaternion.Euler(0f, angle, 0f);
    224.                 //transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
    225.  
    226.                 Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    227.                 _controller.Move(moveDir.normalized * _speed * Time.deltaTime);
    228.             } else {
    229.                 _animator.Play(_rac.animationClips[0].name);
    230.                 // Compensation for animations shifting model location when we reset to idle
    231.                 _avatar.transform.localRotation = Quaternion.Euler(Vector3.zero);
    232.             }
    233.         }
    234.  
    235.         /// <summary>
    236.         /// Produces artificial gravity, as we are not using rigid body physics for the character.
    237.         /// </summary>
    238.         [ToDo("Optimize the gravity code out of the Update loop like with the free look code.", false, 103)]
    239.         protected void ProcessGravity() {
    240.             _yVelocity.y += _gravity / 2 * Time.deltaTime * Time.deltaTime;
    241.             _controller.Move(_yVelocity);
    242.         }
    243.  
    244.         /// <summary>
    245.         /// Handles input configuration once the input provider (a valid InputAction) is initialized.
    246.         /// </summary>
    247.         override protected void HandleInputProviderInit(HiveliminalPrototypePlayerInputActions inputActions) {
    248.             base.HandleInputProviderInit(inputActions);
    249.  
    250.             inputActions.XboxControllerActionMap.AButton.started += context => HandleAButton();
    251.             inputActions.XboxControllerActionMap.RightStick.started += context => HandleStartedRightStick();
    252.         }
    253.  
    254.         /// <summary>
    255.         /// Handles when the 'A' button is pressed.
    256.         /// </summary>
    257.         public void HandleAButton() {
    258.             if (_isJumping) return;
    259.  
    260.             //DebugLogger.Log("JUMP");
    261.  
    262.             _isJumping = true;
    263.         }
    264.  
    265.         /// <summary>
    266.         /// Handles behavior after right stick input is first detected.
    267.         /// </summary>
    268.         public void HandleStartedRightStick() {
    269.             //DebugLogger.Log("START");
    270.  
    271.             if (_startRightStickArgs == null) {
    272.                 _startRightStickArgs = new Hashtable();
    273.                 _startRightStickArgs.Add("delay", 0.0f);
    274.                 _startRightStickArgs.Add("interval", _HANDLE_RIGHT_STICK_INTERVAL);
    275.                 _startRightStickArgs.Add("preferenceInterval", false);
    276.             }
    277.  
    278.             if (_continueRightStickArgs == null) {
    279.                 _continueRightStickArgs = new Hashtable();
    280.                 _continueRightStickArgs.Add("delay", 0.0f);
    281.                 _continueRightStickArgs.Add("interval", _HANDLE_RIGHT_STICK_INTERVAL);
    282.                 _continueRightStickArgs.Add("preferenceInterval", true);
    283.             }
    284.  
    285.             if (!_handlingRightStick) {
    286.                 _handlingRightStick = true;
    287.  
    288.                 //DebugLogger.Log("START ACTUAL | " + _rightStickX + ":" + _rightStickY);
    289.                 //DebugLogger.Log("START ACTUAL");
    290.  
    291.                 StartCoroutine("HandleRightStickMoving", _startRightStickArgs);
    292.             }
    293.         }
    294.  
    295.         /// <summary>
    296.         /// Coroutine to manage right stick input. Optimizes this code out of the update loop.
    297.         /// </summary>
    298.         /// <param name="arguments">Arguments required for the coroutine.</param>
    299.         /// <returns></returns>
    300.         public IEnumerator HandleRightStickMoving(Hashtable arguments) {
    301.             //DebugLogger.Log("COROUTINE");
    302.  
    303.             float delay = 0f;
    304.             float interval = 0f;
    305.             bool preferenceInterval = false;
    306.  
    307.             float.TryParse(arguments["delay"].ToString(), out delay);
    308.             float.TryParse(arguments["interval"].ToString(), out interval);
    309.             bool.TryParse(arguments["preferenceInterval"].ToString(), out preferenceInterval);
    310.  
    311.             float wait = (preferenceInterval == true) ? interval : delay;
    312.  
    313.             yield return new WaitForSeconds(wait);
    314.  
    315.             _freeLookComponent.m_XAxis.Value += _rightStickX * 180f * _xLookSpeed * Time.deltaTime;
    316.             _freeLookComponent.m_YAxis.Value += _rightStickY * _yLookSpeed * Time.deltaTime * _invert;
    317.  
    318.             if ((_rightStickX > _rightstickDeadzoneX || _rightStickX < -_rightstickDeadzoneX) ||
    319.                 (_rightStickY > _rightstickDeadzoneY || _rightStickY < -_rightstickDeadzoneY)
    320.             ) {
    321.                 StartCoroutine("HandleRightStickMoving", _continueRightStickArgs);
    322.             } else {
    323.                 _handlingRightStick = false;
    324.                 _avatar.transform.localPosition = _originalAvatarPosition;
    325.  
    326.                 //DebugLogger.Log("STOP | " + _rightStickX + ":" + _rightStickY);
    327.                 //DebugLogger.Log("STOP");
    328.             }
    329.         }
    330.     }
    331. }
    332.  




    https://smontambault.medium.com/unity-cinemachine-with-new-input-system-b608e13997c7
     
    Last edited: Apr 16, 2021
  26. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    873
    Please work faster why is this still a problem even in unity 2021.2?

    upload_2021-10-27_23-31-20.png

    Like why doesn't have dual compatibility when that's an implemented feature?

    It's incredibly frustrating to finally have to move to the new input system and cinemachine, a package meaning modifying the existing scripts is difficult, is broken.

    AAaaaugh i don't have time to make a whole new damn camera script!

    Like bro it's been a year, HOW. This thread has 11K views jesus dude.

    Yall can't just add like a "if input system" to that aspect???

    Best I can find is "Cinemachine Input Provider" but that doesn't even seem to work. Why make this roundabout?
     
    Last edited: Oct 28, 2021
    MaxRoetzler and Regueiro96 like this.
  27. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    873


    Ok this video kind of explains the process, but it's rather specific, I ended up doing it wrong earlier.

    I'm still getting errors though when i right click.

    I understand the mentality was making it compatible with any input system. But for something base like the input system that just breaks everything i'm 100% sure you could come up with a more streamlined solution. Cause I imagine stuff like making cinemachine components compatible with Standard, URP and HDRP was a more difficult task than this would be.
     
  28. pkumarshall

    pkumarshall

    Joined:
    Aug 3, 2018
    Posts:
    11
    I updated my project from 2021.1.23 to 2021.2.0, then the Cinemachine Input Provider seemed not working with CinemachineFreelook any more. After some testing, I found that I have to delete the Input Provider first in editor mode, then press the Add Input Provider Button of CinemachineFreelook in game mode to make the CinemachineFreelook X Axis controlled by RightSticks[Gamepad] successfully. I'm not sure if my test result is right, please help me, thanks.
     
  29. ngerbens

    ngerbens

    Joined:
    Nov 20, 2017
    Posts:
    33
    [Post Editted] A really good explanation on combining Cinemachine and the New Input System is found here:
     
    Last edited: Jan 30, 2022
  30. Oknees

    Oknees

    Joined:
    Oct 22, 2021
    Posts:
    7
    Sorry for reviving this thread but I have problems with the CInemachine Input Provider. It's not that it doesn't work, it's because it feels like dog water to control.

    For example, in most games I have experienced when you move your joystick, or mouse or whatever you're using, a little bit the camera moves slowly, and when you push the joystick fully or move the mouse quickly the camera moves much faster, giving control to the player.

    However, with the Cinemachine Input Provider, that doesn't happen. Instead, the camera moves at the same speed weather you're moving the joystick fully or partially. That leads to the player either overshooting, or undershooting, where they wanted the camera to look at.

    I'm not sure if this is a problem with Unity's new Input System, or a problem with the CInemachine Input Provider.

    Also these are the Input Actions I used to control the camera for mouse and joystick

    upload_2022-3-20_22-16-49.png

    CamMovement's properties

    upload_2022-3-20_22-17-44.png

    When not using the Input Provider script the camera works normally i.e. the camera moves slowly when the mouse moves slowly and faster when the mouse moves rapidly, however, you can't easily use multiple input sources and I want to stick with using the new input system.

    So anybody got any solutions to this because I don't want to use the old input system but I don't want the camera to feel bad to control.

    Thanks :)
     
  31. Oknees

    Oknees

    Joined:
    Oct 22, 2021
    Posts:
    7

    Okay wow the solution was so simple I'm crying right now whilst typing it.

    In the Properties tab just disable normalise vector 2

    Before


    After
    upload_2022-3-22_16-45-42.png

    If anybody out there was having the same problem as the post I made and replied to just look at this before doing something crazy. Thank goodness I caught myself now before it got any worse and end up noodling.
     
  32. SnowyStxrm

    SnowyStxrm

    Joined:
    Oct 2, 2021
    Posts:
    1
    I had this system where it would only activate the mouse axis on a right click, because I would use a right click to move the camera. The code is the following:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         CinemachineCore.GetInputAxis = GetAxisCustom;
    4.     }
    5.  
    6.     public float GetAxisCustom(string axisName)
    7.     {
    8.         if (axisName == "Mouse X")
    9.         {
    10.             if (Input.GetMouseButton(1))
    11.                 return -Input.GetAxis("Mouse X");
    12.             else
    13.                 return 0;
    14.         }
    15.         else if (axisName == "Mouse Y")
    16.         {
    17.             if (Input.GetMouseButton(1))
    18.                 return -Input.GetAxis("Mouse Y");
    19.             else
    20.                 return 0;
    21.         }
    22.         return UnityEngine.Input.GetAxis(axisName);
    23.     }
    However when I updated to the InputSystem, I could not figure out how to update this. If anyone could help that would be much appreciated. The right click worked fine before I updated.

    More info: I am using the Input Action Asset so I have no physical object with code.
     
  33. Nileshk1080

    Nileshk1080

    Joined:
    Jul 4, 2022
    Posts:
    3
    Hey, i am working for Android
    I want to rotate cinemachine camera so I am using new input system and on ui i added on stick function in i selected left stick gamepad and i maded new input action and added left stick binding.......
    So when I rotate camera by ui and i am not realising mouse/finger and stop at that position of mouse/finger that time camera rotating continuously, how can I stop this camera rotating continuously?

    Please help me
     
    Last edited: Jul 13, 2022