Okay, but how do we ACTUALLY MOVE THE CAMERA? Step 3: Move the camera This is why we are working with the assumption that the camera starts looking "forward", and will rotate around the X and Y axes.Īt the end of the function, don't forget to store our currentPosition as our previousPosition! In UnityEngine, the forward vector is by convention (0, 0, 1). (1 / 180º) = (direction / rotationAroundAxis)
![spine2d flip unity3d spine2d flip unity3d](https://i.ytimg.com/vi/xvmw0YsSmk4/maxresdefault.jpg)
They are calculated using this very advanced mathematical formula:
![spine2d flip unity3d spine2d flip unity3d](https://i.ytimg.com/vi/W8BDM_jth3Y/maxresdefault.jpg)
RotationAroundYAxis and rotationAroundXAxis are the rotations we must add to the current rotation each frame. In each subsequent frames, we calculate direction, which is the difference between the current mousePosition and previousPosition. On the first frame in which the mouse is down (or the finger touches the screen), we store that position as our previousPosition. Vector3 direction = previousPosition - currentPosition įloat rotationAroundYAxis = -direction.x * 180 // camera moves horizontallyįloat rotationAroundXAxis = direction.y * 180 // camera moves vertically Vector3 currentPosition = cam.ScreenToViewportPoint(Input.mousePosition) PreviousPosition = cam.ScreenToViewportPoint(Input.mousePosition)
#SPINE2D FLIP UNITY3D CODE#
This is how the code looks: public class CameraMovement : MonoBehaviour In order to do that, we need to store the previous position in a private field ( previousPosition). Step 2: Calculate the rotationĮvery frame we will calculate how much the cursor/finger moved since last frame, and translate it into how many degrees the camera should rotate according to our previously defined relation:ġ unit in viewport coordinates 180 degrees of camera rotation Now that we have the problem defined and the drag detected, we can move the camera. The Input class also has a convenient mousePosition field which gives us the position of the pointer (mouse or finger) in screen coordinates. We are using the functions Input.GetMouseButtonDown(0) and Input.GetMouseButton(0) to detect whether the user clicked/tapped or dragged the pointer over the screen.
#SPINE2D FLIP UNITY3D UPDATE#
(It is also good practice not to handle it in the Update function directly, but this is the smallest of projects and we're not going to over-engineer it.) In Unity games, it is common practice to use the Update function to detect player input. Will be true as long as the mouse is down or a touch is happening.
![spine2d flip unity3d spine2d flip unity3d](https://miro.medium.com/max/1666/1*srBw68JwCVqG-z79QokHBw.png)
Will be true only in the 1st frame in which it detects the mouse is down (or a tap is happening) Open CameraMovement.cs with your editor, time to code! public class CameraMovement : MonoBehaviour Of course you can arrange your files and classes however you prefer.) (This is the most basic setup working with an empty project.
![spine2d flip unity3d spine2d flip unity3d](https://s3.amazonaws.com/gameartpartnersimagehost/wp-content/uploads/2021/03/sprites.png)
This created a " CameraMovement.cs" file in your Assets folder, which contains an empty CameraMovement class that extends MonoBehaviour. In the InputController's Inspector, click "Add Script".Since a drag from left to right of the screen will have a distance of 1, we have just defined our relation:ġ unit in viewport coordinates 180 degrees of camera rotation Step 1: Detect the drag (= user input)įor this, I created an empty GameObject in the scene (I named it " InputController"), and added a script to it (that I created, and named " CameraMovement"). The screen (the viewport rather) has a size of 1x1 in viewport coordinates.