How to create Unity3d Cinemachine 3rd person mobile swipe camera?

1k Views Asked by At

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?

#region Variables
[SerializeField]
private float playerSpeed = 2.0f;
[SerializeField]
private float jumpHeight = 1.0f;
[SerializeField]
private float gravityValue = -9.81f;
[SerializeField]
private float rotationSpeed = 0.1f;
private float rotationVelocity;

private Transform cameraMain;
private Transform child;
private Player playerInput;
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
#endregion

#region Main Methods
private void Awake()
{
    playerInput = new Player();
    controller = GetComponent<CharacterController>();
}

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

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


private void Start()
{
    cameraMain = Camera.main.transform;
    child = transform.GetChild(0).transform;
}

void Update()
{
    groundedPlayer = controller.isGrounded;
    if (groundedPlayer && playerVelocity.y < 0)
    {
        playerVelocity.y = 0f;
    }

    Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>();
    //Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
    Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
    move.y = 0f;
    //controller.Move(move * Time.deltaTime * playerSpeed);

    if (move != Vector3.zero)
    {
        float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg;
        float angle = Mathf.SmoothDampAngle(child.eulerAngles.y, targetAngle, ref rotationVelocity, rotationSpeed);
        child.rotation = Quaternion.Euler(0f, angle, 0f);
        controller.Move(move * Time.deltaTime * playerSpeed);
    }

    // Changes the height position of the player..
    if (playerInput.PlayerMain.Jump.triggered && groundedPlayer)
    {
        playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    }

    playerVelocity.y += gravityValue * Time.deltaTime;
    controller.Move(playerVelocity * Time.deltaTime);

    
}

Above is the player controller code learned recently from youtube tutorial. Below is input action map and gameObject for the gamepad: enter image description here

enter image description here

1

There are 1 best solutions below

2
On

You can simply fix it using touchscreen(delta) in new input managager and in the onstick function of drag image manage the input.