Search Unity

How to create third person mobile swipe camera

Discussion in 'Input System' started by Airies, May 13, 2021.

  1. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    I have two controls. Left joystick on screen to move the character. And touch anywhere else, but the joystick, to move the freelook. I don't want the left joystick motion to affect the freelook. How can I setup these inputs so they work independently. I want the user to have complete control of the camera that moves around the player. The user has to press and hold the screen which will then hold on to the camera and move around the player. Attached is a file where I have created the camera using cinemachine. I used a right - stick gamepad where the user can move the camera around the player. It works very well, the only problem I have that is when the user moves it to the right or left while still pressing, it will automatically rotate around the player, which I do not want.
    I asked this question in the cinemachine forum, but was told it was better here, as it is an input system question.
    Attached below is the player controller code:

    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. private Transform cameraMain;
    12. private Transform child;
    13. private Player playerInput;
    14. private CharacterController controller;
    15. private Vector3 playerVelocity;
    16. private bool groundedPlayer;
    17. #endregion
    18. #region Main Methods
    19. private void Awake()
    20. {
    21.     playerInput = new Player();
    22.     controller = GetComponent<CharacterController>();
    23. }
    24. private void OnEnable()
    25. {
    26.     playerInput.Enable();
    27. }
    28. private void OnDisable()
    29. {
    30.     playerInput.Disable();
    31. }
    32. private void Start()
    33. {
    34.     cameraMain = Camera.main.transform;
    35.     child = transform.GetChild(0).transform;
    36. }
    37. void Update()
    38. {
    39.     groundedPlayer = controller.isGrounded;
    40.     if (groundedPlayer && playerVelocity.y < 0)
    41.     {
    42.         playerVelocity.y = 0f;
    43.     }
    44.     Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>();
    45.     //Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
    46.     Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
    47.     move.y = 0f;
    48.     //controller.Move(move * Time.deltaTime * playerSpeed);
    49.     if (move != Vector3.zero)
    50.     {
    51.         float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg;
    52.         float angle = Mathf.SmoothDampAngle(child.eulerAngles.y, targetAngle, ref rotationVelocity, rotationSpeed);
    53.         child.rotation = Quaternion.Euler(0f, angle, 0f);
    54.         controller.Move(move * Time.deltaTime * playerSpeed);
    55.     }
    56.     // Changes the height position of the player..
    57.     if (playerInput.PlayerMain.Jump.triggered && groundedPlayer)
    58.     {
    59.         playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    60.     }
    61.     playerVelocity.y += gravityValue * Time.deltaTime;
    62.     controller.Move(playerVelocity * Time.deltaTime);
    63.  
    64. }
    I also have images of the input system action map that I am using:

     

    Attached Files:

    Last edited: May 19, 2021
  2. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    Your camera is moving with the Move Action of the player not the Look Action form the Map input. you need to give the camera the input playerInput.Player.Look
     
  3. Airies

    Airies

    Joined:
    Oct 11, 2019
    Posts:
    11
    thank you for your reply, I am sorry but I do not understand what you mean because I am using cinemachine and I think I have already done in the cinemachine input provider as shown below:
    upload_2021-5-25_13-33-44.png

    Along with my main camera:
    upload_2021-5-25_13-34-24.png