Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Disable free look camera movement when touch detected on mobile joystick

Discussion in 'Cinemachine' started by Vagonn, Jun 29, 2020.

  1. Vagonn

    Vagonn

    Joined:
    Aug 1, 2014
    Posts:
    5
    I'm using free joystick pack form asset store and in scene have two cameras (first person and third person). Switching cameras with toggle.

    Game starts with first person camera. Moving with fixed joystick and rotating camera with swiping on right side of screen. Everything is ok for this step.

    For third person camera I'm using cinemachine free look camera. Cinemachine camera is reacting to joystick touch inputs. I need to disable camera movement when interacting with joystick. Tested on Android device

    Cinemachine settings:



    Third Person movement script:


    Code (CSharp):
    1. void Update()
    2. {
    3. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
    4.             {
    5.                 x = joystick.Horizontal;
    6.                 z = joystick.Vertical;
    7.             }
    8.             else
    9.             {
    10.                 x = Input.GetAxis("Horizontal");
    11.                 z = Input.GetAxis("Vertical");
    12.             }
    13.  
    14.             Vector3 direction = new Vector3(x, 0, z).normalized;
    15.  
    16.             if (direction.magnitude >= 0.1f)
    17.             {
    18.                 float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + camTps.transform.eulerAngles.y;
    19.                 float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    20.                 transform.rotation = Quaternion.Euler(0f, angle, 0f);
    21.  
    22.                 Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    23.                 characterController.Move(moveDirection.normalized * speedTps * Time.deltaTime);
    24.             }
    25. }
    Any ideas and suggestions is appreciated
     
  2. Vagonn

    Vagonn

    Joined:
    Aug 1, 2014
    Posts:
    5
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    Setting the "Input Axis Name" fields to blank will stop the FreeLook from reading user input. You can then control FreeLook.m_XAxis.Value and FreeLook.m_YAxis.Value any way you like. Those fields will control how the FreeLook positions itself in relation to the target.
     
  4. Vagonn

    Vagonn

    Joined:
    Aug 1, 2014
    Posts:
    5
    Removed Mouse X and Mouse Y from Input Axis Name fields. And added this code to update m_XAxis and m_YAxis values

    Code (CSharp):
    1. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
    2.             {
    3.                 x = joystick.Horizontal;
    4.                 z = joystick.Vertical;
    5.             }
    6.             else
    7.             {
    8.                 x = Input.GetAxis("Horizontal");
    9.                 z = Input.GetAxis("Vertical");
    10.             }
    11.  
    12.             Vector3 moveDirection = transform.right * x + transform.forward * z;
    13.             characterController.Move(moveDirection * speed * Time.deltaTime);
    14.  
    15.             cinemachineFreeLook.m_XAxis.Value = Input.GetAxis("Horizontal");
    16.             cinemachineFreeLook.m_YAxis.Value = Input.GetAxis("Vertical");
    Players turns, but third person camera not rotating. Added short clip of app below



    Interesting, what I'm doing wrong?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    Code (CSharp):
    1. cinemachineFreeLook.m_XAxis.Value = Input.GetAxis("Horizontal");
    2. cinemachineFreeLook.m_YAxis.Value = Input.GetAxis("Vertical");
    Is wrong. Axis.Value represents the current state of the axis, not the state of the input. If you want CM to control the axis based on user input, then set axis.InputValue = Input.GetAxis(). Otherwise you need to process the input yourself and interpret it to generate an appropriate axis value.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    If you always want the 3rd-person camera to rotate with the player, change the FreeLook's binding mode. Right now you have it at WorldSpace, so the X axis is relative to world coords. If you set it to LockToTargetWithWorldUp, then the axis will be relative to player forward, and the camera will automatically preserve its relationship with the player's rotation, without having to change the axis values
     
    Vagonn likes this.
  7. Vagonn

    Vagonn

    Joined:
    Aug 1, 2014
    Posts:
    5
    this works correctly. But it only showing player from back. I need to rotate camera around player and when pressing forward from joystick, player turn himself where camera is looking
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    In that case you'll need to stay with worldspace binding mode and add code to keep the freelook's x-axis appropriately in sync with the player's, according to the needs of your game.

    In LockToTargetWithWorldUp mode, changing the axis value will rotate the camera around the player. You can do it that way too. 0 is behind.
     
  9. Vagonn

    Vagonn

    Joined:
    Aug 1, 2014
    Posts:
    5
    Is there any example how to update freelook axis values with touch input?
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    Touch input is not different from other input. If you set the axis.inputValue to (some input), then FreeLook will rotate.

    Alternatively, you can just manage the axis.Value yourself. The units are in degrees from +Z. Which co-ordinate space is used will depend on the binding mode.
     
unityunity