From the course: Unity Medical Visualization: 03 Deployment and Usability

Installing mobile kits - Unity Tutorial

From the course: Unity Medical Visualization: 03 Deployment and Usability

Start my 1-month free trial

Installing mobile kits

- [Instructor] In the previous section of this course, we imported the Cinemachine library and we configured our FreeLook camera which you can already see positioned here inside the scene hierarchy. Now, by using this camera, we can link our input axes to the camera that is facing the heart so we can control it directly using no code at all. And by default, input is linked to the mouse but we went ahead and changed that to the vertical and the horizontal axes so we could control motion using the keyboard and in particular, the W, A, S and D keys or the left and right arrows and the up and down arrows and that's great but we still want more control. I want to be able to control rotation around the heart using both the mouse and the keyboard, if we choose to do so, and also to have the flexibility to tweak it for different types of input devices and when we port our visualization to a mobile device, like Android or iOS. Now, to do this, we need to create our own script file to customize the behavior of the FreeLook camera. So I'm going to do that by jumping over to the Scripts folder, right clicking, choosing Create C# Script and I'm going to call this script InputAxisControl and press Enter on the keyboard and then double click that to bring that into Visual Studio. As expected, the script file we've created is derived from MonoBehaviour, completely as expected but I want to include two additional namespaces in this script file that we're going to be making use of. They are the Cinemachine namespace. So I'm going to type using Cinemachine. And actually, I am actually going to just save my code and return back and resume recording in just a second because I notice the code completion is not working as intended. Okay so I just quit Visual Studio and restarted it. Sometimes that happens. Code completion doesn't always work. But now it's back. So I'm going to type using UnityEngine.EventSystems. Perfect. And that's in there too. Now, I'm going to delete the Update and the Start function. We don't need these just yet. The first thing that I need to do is I need to link Cinemachine to a custom function in our InputAxisControl class that is going to feed Cinemachine the input that we read from the keyboard or wherever we want. So to get started at doing that, I'm going to create the Awake function here. So I'm going to add the Awake function, private void Awake and this is going to run whenever the level begins and the first thing I want to do is to create a function in our class that is going to work alongside it. This function can be named anything you want but it needs to take a particular form that Cinemachine specifies. So it has to return a float. I'm going to call it GetAxisCustom. Great. And it takes one input, which is a string, and again, that can be named anything you want. For now, I'm just going to return a value of zero F. We're going to be implementing the function a little bit later. Now, I'm going to move back up to the Awake function and inside the Awake function, I'm going to access the CinemachineCore. So that's CinemachineCore.GetInputAxis and it's going to equal this function. It's a delegate. It's going to be initiated whenever Cinemachine needs to read input. I'm going to save my code. Minimize that and return back to Unity here. Grab my FreeLook camera and this has to be the same object that has the Cinemachine FreeLook Cinemachine and I'm going to drag and drop my InputAxisControl onto that there. And press play on the toolbar. Now, when I press play on the toolbar, we see the heart visualization just as before but now if I use the keys on the keyboard, W, A, S and D, or the arrow keys, even though I've mapped the input to the vertical and the horizontal axes, nothing is happening. And that is because the function that I've just created is completely overriding the input that Cinemachine is expecting. Right now our function just returns a value of zero. And as a result of that, nothing is changing. But this is the key piece of the puzzle because in creating this function, we can return any value we want and completely override the input system for the Cinemachine FreeLook camera. In the next movie, we're going to see how to use this function in more depth.

Contents