articlesnomad.blogg.se

How to rotate drawing in viewport
How to rotate drawing in viewport










how to rotate drawing in viewport
  1. HOW TO ROTATE DRAWING IN VIEWPORT UPDATE
  2. HOW TO ROTATE DRAWING IN VIEWPORT CODE

(1 / 180º) = (direction / rotationAroundAxis)

how to rotate drawing in viewport

They are calculated using this very advanced mathematical formula: 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)

HOW TO ROTATE DRAWING IN VIEWPORT 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.

HOW TO ROTATE DRAWING IN VIEWPORT 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. 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.

how to rotate drawing in viewport

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"). Here is my script: using System.The screen (the viewport rather) has a size of 1x1 in viewport coordinates. That is the object with the camera on it, all I need to do is figure out a way to rotate it 180 on the Y, and then on unclick make it go backto 0. In my script I have a gameobject called "you". My game is a being chased FPS, so I want to be able to look behind me, while keep running forward, this is possible, I've tried it by rotating the object the camera is on in the inspector and it works, so all I have to do is roate that object. So I want a system like the ones that are in many racing games, where when you press middle mouse (and while it is held down), the camera shows whats behind you (rotates 180 on the Y axis).












    How to rotate drawing in viewport