Home > OS >  Strange behaviour of the Cinemachine virtual camera
Strange behaviour of the Cinemachine virtual camera

Time:11-14

I am using the new input system in my unity project. I also use Cinemachine. I use Cinemachine Input Provider to change the input from the old to the new system. When I change max speed to Input Value Gain in speed field of virtual camera's settings (I did it because it is the most comfortable way to control camera) I face a problem. My problem: When my character moves after some time the camera speed changes. If I start to move in the opposite direction, the camera speed returns to normal. This is independent of the other components in the scene. My scene has only plane, cube, camera and my character.

Here's my character control code (ignore the awful calculation of movement direction):

private Rigidbody _rb;

private Vector2 _moveDirection;

private float speed = 5f;

private void Awake()
{
    _rb = GetComponent<Rigidbody>();
    Cursor.lockState = CursorLockMode.Locked;
}

public void OnMove(InputAction.CallbackContext context)
{
    _moveDirection = context.ReadValue<Vector2>();
}

private void FixedUpdate()
{
    Move(_moveDirection);
}

private void Move(Vector3 moveDirection)
{
    float scaledMoveSpeed = speed * Time.deltaTime;

    moveDirection = new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z).normalized  * moveDirection.y   new Vector3(Camera.main.transform.right.x, 0, Camera.main.transform.right.z).normalized * moveDirection.x;
    _rb.MovePosition(transform.position   moveDirection * scaledMoveSpeed);
}

Here's a screenshot of the camera settings and Cinemachine Input Provider:

enter image description here

And screenshots of the Input Actions settings:

enter image description here

enter image description here

CodePudding user response:

I found a solution for those who will ever face this problem! On your main camera, in the CinemachineBrain component, change the Update Method from Smart Update to Late Update. That should help

  • Related