3rd Person Controller Using Cinemachine Virtual Camera On Mobile
Tags: tutorial, unity3d, gamedev, mobile
Published: 2021-12-02 16:26:01

Going to share the approach I’m using in having a 3rd person controller using Cinemachine Virtual Camera by controlling it using mobile Input.

YouTube: https://youtu.be/88855QY4CXk

GitHub: https://github.com/rehtse-studio/Cinemachine_MobileInput

Starter Assets — Third Person Character Controller: https://assetstore.unity.com/packages/essentials/starter-assets-third-person-character-controller-196526

## **What Do We Have On This Project?**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0063ibuzn936uldib7dw.png)

On the Hierarchy I have a the Player GameObject called _PlayerWithVirtualCamera_, on it you will find components like the Animator, SphereCollider and a Rigidbody; it is also hold the script called _PlayerControllerWithVirtualCamera _. I also have the PlayerInput component that will allows me to use the Input Actions called VCamera3rdPersonInputActions from Unity’s New Input System.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80zs58yd64prht38bg8c.png)


The VirtualCamera_UI GameObject hold two UI Canvas: UI_Standard and UI_Mobile

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7w2mzvufjfuf4548zd93.png)

UI_Standard canvas hold the buttons to make the effect that I’m turning the Mobile Input ON or OFF.

UI_Mobile canvas has the necessary UI GameObject that hold the Mobile Input logics. A UI Panel called LookTouchPanel hold the script UITouchPanel that will create the necessary value X and Y to control the camera on mobile.

A UI Image called JoystickContainer has a script that create the X and Y value to move the player, the script is called UIVirtualJoystick.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvoj4m3isifsfzbu5f07.png)

There is another UI Image named RunAction that has the On-ScreenButton script from the new Input System and it will behave like a GamePad LeftTrigger establish by the Control Path.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhidigksheblgi0srb13.png)

Inside the players GameObject I’m holding the Cinemachine Virtual Camera and a empty GameObject CameraTargetObject. The CameraTargetObject is the LookAt and the Follow of the CinemachineVirtualCamera that will allow me from the script to rotate; this empty game object is near the chest area of the player.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4yyquzbe7p1p8drjm5w.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea5g0r6deh07sldadvge.png)


##**Scripts**

_PlayerControllerWithVirtualCamera_
On the script I have the necessary variables and reference to control my player and to get the inputs from the virtual joystick, the touch panel and from the new Input System.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8pbpu6c3xz3ley6k8oi.png)

_UIVirtualJoystick_
**NOTE: The logic for the Joystick was studying using Unity’s New Asset that has different mobile environment for Input**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jb6j34uyp786537mvlu.png)

_UITouchPanel_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcl3vhxigsy0wqx8x7dn.png)

At the beginning of the article you have links for the Unity project and the video from YouTube.

Hope it helps.

To be continued 😎