Setting this will make the top “head” of the object align to a direction. Setting this aligns the right side of the object to a direction. The pause is caused by the video refresh.Īnother way of setting the rotation directly is by setting any of the following properties to a directional vector: It rotates the cube 90 degrees horizontally each second.
This will set the rotation to a new rotation based on an increasing Y axis. This means it is an accurate way of counting upwards. Deltatime is the amount of time it takes for a frame to pass This will make it move 90 degrees per second Public class RotateHorizontally : MonoBehaviour The quickest way to create a new Quaternion struct is by calling a static function that has been provided by Unity. You can do this by setting “transform.rotation” to a new Quaternion. The quickest way of directly modifying a rotation of a object is by setting it directly. } Example of rotating using axis (left: up, mid: forward, right: right) Setting the rotation directly As deltatime is the completion time of a frame.įloat step = degreeStep * ltaTime Use deltaTime to make the results the same for any Public class RotationExamples : MonoBehaviour Public void Rotate(Vector3 axis, float angle) Rotate based on an axis, with an added angle Public void Rotate(float xAngle, float yAngle, float zAngle) Rotate the object based on added angles All the methods below also allow you to set a space for the rotation. Methodologies without additive rotations can be found next on this page. The rotate method has a lot of use cases, although be mindful that it always adds a rotation. The easing of objects can often be a hassle to code, because of this I reccomend using a tweening library like DoTween. In case you want to ease the rotation to gradually start looking at a position you can use “Quaternion.LookRotation(location, up)” and use “Quaternion.Lerp(rotA,rotB,t)”. In case you want to make a object face another object directly you can call “(location)”. So in order to make a object look to the right of your screen you would call “ = Quaternion.Euler(0,-90f,0) ”.
If you want to use vectors to set a gameObject’s rotation you have to use Quaternion.Euler(x,y,z). Altough you cannot simply set this to an vector. The most common way to move an object in Unity is to set the rotation of an object by accessing gameObject.rotation. In case you haven’t read the post about moving an object, you can find it here. Here is a small writeup on the most common ways to move objects, both oriented towards 2D and 3D. Sometimes you want objects to look at another object, or to face a specific direction when they are moving. Find more info here.Aside from movement, rotations are also often required in games. The screen-space comes very handily in this case because it emulates the “first quadrant” of a “Cartesian plane”, where all the x and y are positive. The easiest way to accomplish this is to move from the world-space (measured in units) to the screen-space (measured in pixels). In order to use this simplified version in Unity, we need to apply some tweaks. That gives us the following:Īnd by replacing slopes with their respective values, we end up with: Simply by considering the first line as the x-axis, and the second one (the one pointing to the mouse) as passing from zero. And so, given two lines:Īnd their respective slopes m 1 and m 2, the angle between them is given by:Īll right but, how do we go from the formula above to the Unity’s one which basically is:
To understand that, we need to dust off some old mathematics memories and make use of some linear equation formulas. Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).Īnd that’s why, at line 7, we provide the x and y of the mouse position (multiplying everything by the radians-to-degrees conversion constant). Returns the angle in radians whose Tan is y/x. Let’s start with the definition Math.Atan2 that you can find in the Unity documentation: Player.rotation = Quaternion.Euler(new Vector3(0, 0, angle)) Var angle = Mathf.Atan2(mouseScreenPos.y, mouseScreenPos.x) * Mathf.Rad2Deg Var startingScreenPos = mainCamera.WorldToScreenPoint(player.position) Var mouseScreenPos = Input.mousePosition Private void PointToMouse(Transform player)