Search Unity

Unity3d Cinemachine 3rd person mobile swipe camera

Discussion in 'Cinemachine' started by Airies, Apr 19, 2021.

  1. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    I have created a 3rd person freelook camera along with a player controller that takes input from the new input system action map, where it is from the left stick gamepad, jump and can look; thanks to a youtube tutorial. The camera is working great where I rotate around my player and the player moves in the direction of the camera.
    After building and playing on my phone, I realised there's a problem, the camera does not stop rotating as long as the user does not stop pressing the screen. I do not want that as it starts to get annoying the more the user plays the game. I want the user to press and hold the camera and rotate it as they are dragging it around the player (like in the mobile game genshin impact). I do not know the proper term for it, but I assumed it is a swipe 3rd person camera. Can some one tell me how it is done?
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    How did you setup your input? Could you attach some images of it?
     
  3. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    Code (CSharp):
    1. #region Variables
    2. [SerializeField]
    3. private float playerSpeed = 2.0f;
    4. [SerializeField]
    5. private float jumpHeight = 1.0f;
    6. [SerializeField]
    7. private float gravityValue = -9.81f;
    8. [SerializeField]
    9. private float rotationSpeed = 0.1f;
    10. private float rotationVelocity;
    11.  
    12. private Transform cameraMain;
    13. private Transform child;
    14. private Player playerInput;
    15. private CharacterController controller;
    16. private Vector3 playerVelocity;
    17. private bool groundedPlayer;
    18. #endregion
    19.  
    20. #region Main Methods
    21. private void Awake()
    22. {
    23.     playerInput = new Player();
    24.     controller = GetComponent<CharacterController>();
    25. }
    26.  
    27. private void OnEnable()
    28. {
    29.     playerInput.Enable();
    30. }
    31.  
    32. private void OnDisable()
    33. {
    34.     playerInput.Disable();
    35. }
    36.  
    37.  
    38. private void Start()
    39. {
    40.     cameraMain = Camera.main.transform;
    41.     child = transform.GetChild(0).transform;
    42. }
    43.  
    44. void Update()
    45. {
    46.     groundedPlayer = controller.isGrounded;
    47.     if (groundedPlayer && playerVelocity.y < 0)
    48.     {
    49.         playerVelocity.y = 0f;
    50.     }
    51.  
    52.     Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>();
    53.     //Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
    54.     Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
    55.     move.y = 0f;
    56.     //controller.Move(move * Time.deltaTime * playerSpeed);
    57.  
    58.     if (move != Vector3.zero)
    59.     {
    60.         float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg;
    61.         float angle = Mathf.SmoothDampAngle(child.eulerAngles.y, targetAngle, ref rotationVelocity, rotationSpeed);
    62.         child.rotation = Quaternion.Euler(0f, angle, 0f);
    63.         controller.Move(move * Time.deltaTime * playerSpeed);
    64.     }
    65.  
    66.     // Changes the height position of the player..
    67.     if (playerInput.PlayerMain.Jump.triggered && groundedPlayer)
    68.     {
    69.         playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    70.     }
    71.  
    72.     playerVelocity.y += gravityValue * Time.deltaTime;
    73.     controller.Move(playerVelocity * Time.deltaTime);
    74.  
    75.    
    76. }
    Above is the player controller code learned recently from youtube tutorial. Below is input action map and gameObject for the gamepad:

    upload_2021-4-20_1-39-18.png
    upload_2021-4-20_1-39-40.png
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    What is the value of movementInput when you hold your finger on the screen without swiping?

    Are you using CinemachineInputProvider to control freelook?
     
  5. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    Thank you for replying, and yes I am using it, attached below:

    upload_2021-4-21_1-22-39.png
     
  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    The reason why I am asking this is because if it is position vector or a position delta vector.

    If it is a position vector, then you could do the following to stop that script.
    Code (CSharp):
    1. Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>();
    2. Vector2 movementInputDelta = movementInput - prevMovementInput ;
    3.  
    4. prevMovementInput  = movementInput;
    5.  
    6. if (movementInputDelta.sqrtMagnitude() <= epsilon) {
    7.     return;
    8. }
     
  7. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    I have checked (because I learnt this from a youtube tutorial), that this is actually a delta vector, and also where did you get the 'prevMovementinput'?
     
  8. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    In that case, could you send your project as a bug report?
    We are going to check what's wrong.
    > How to report bugs <


    That code was just to calculate the delta, if your input is position based. You can ignore it, because your input gets delta vector.
     
  9. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    I have already created a bug report, I would have sent sooner, but it took too long to upload
     
  10. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    Hello, I have already sent the bug report, and i was redirected to fogbugz page where my case is there, how do I know that there is a reply to it?
     
  11. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I see the report. I'll have a look
     
    Airies likes this.
  12. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    The problem is how the input system is setup. Your Look input is setup to be value, instead of delta.

    In your input settings, under Look, you need to set Delta [Touchscreen] instead of Right Stick [Gamepad]. When I change your Look input setting to Delta [Mouse], I can rotate the camera properly. That is, if I don't move the mouse the camera does not rotate, which should be equivalent to touching the screen, but not moving my finger.

    See the image:
    Screen Shot 2021-05-06 at 2.51.26 AM.png

    To find your input settings, you can click on the CM Input Providers XY Axis field:
    Screen Shot 2021-05-06 at 2.53.11 AM.png

    Otherwise, your Cinemachine setup is good. Good luck with the project! :)
     
    Last edited: May 6, 2021
    Gregoryl likes this.
  13. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    Thank you for your reply, I did as you said and it does work somewhat, the problem now is that the camera rotates as the player moves automatically as opposed to the complete control of the user. Ive tried it as well with the delta mouse, and it still had that jerky motion, and the range of camera rotation seemed limited as well
     
  14. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Could you send a recording of the issue?

    I don't see this in your project. Did you modify your Freelook setup?
    Maybe you forgot to delete Delta [Mouse] from input settings, and now when you drag the little touch UI, you are also providing mouse delta input. Your input settings should only have Delta [Touchscreen] at the end.
     
  15. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    I have attached two zip files

    The first one is where I followed your teaching where I had to change the input setting from right-stick gamepad to delta touchscreen. As you can see, the camera is tortating from where the player is heading, which means the user has no control of the camera. And the range is short when I try to rotate it. I changed it like so :
    upload_2021-5-8_20-23-31.png

    The second file is where I am using the right-stick gamepad. It works perfectly as the user has complete control of the camera, and the player moves in the direction of the camera. The problem is (in the video), when the user press and hold the camera, it will automatically rotate around the player, the camera does not stop and hold.
     

    Attached Files:

  16. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Oh, I understand the problem.

    This question is for the Input System forum: https://forum.unity.com/forums/input-system.103/.

    You have two controls. Left joystick on screen to move the character. And touch anywhere else, but the joystick, to move the freelook. You don't want the left joystick motion to affect the freelook. How can you setup these inputs so they work independently. Ask on the input forum. ;)
     
    Airies likes this.
  17. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    Thank you for your reply and helpfulness, I will do just that
     
  18. Binary42

    Binary42

    Joined:
    Aug 15, 2013
    Posts:
    207
    Hi @Airies, i have a similar issue. Is there a follow up thread in the Input System forum or did you even solve the problem?