DEV Community

Cover image for 3rd Person Controller Using Cinemachine Virtual Camera On Mobile
Esther
Esther

Posted on

3rd Person Controller Using Cinemachine Virtual Camera On Mobile

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

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

The VirtualCamera_UI GameObject hold two UI Canvas: UI_Standard and UI_Mobile

Image description

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

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

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

Image description

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

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

Image description

UITouchPanel

Image description

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

Hope it helps.

To be continued 😎

Discussion (0)