Unity Gyroscope Usage Moving 2D Objects
Unity Gyroscope Usage Moving 2D Objects
Hello friends, in this post I will talk about a command sequence that we can use to move objects in our games made with Unity 3D, especially in our mobile games, and I will show you how to move an object using the device's gyroscope feature without using buttons, just with phone movements.First of all, I want to tell you what a gyroscope does because I think it is better to understand the logic before jumping directly to the codes. A gyroscope is a device that measures the rotation speed of the movement made on the x, y, and z axes, and our phones have a gyroscope feature as well.
Using the Gyroscope in Portrait Mode
Now, create an object in Unity and then assign a script to the object you created, and when you add the codes below, you can move the object you created by tilting your phone left, right, forward, and backward when you hold your phone upright.float speed = 1f;
void Update(){
Input.gyro.enabled = true;
this.transform.Translate(Input.gyro.rotationRateUnbiased.x * speed * Time.deltaTime, Input.gyro.rotationRateUnbiased.y * speed * Time.deltaTime, 0);
}"Input.gyro.enabled = true;" means we want the gyroscope feature of the device to be enabled.
The variable we defined as speed of type float is the value determining the speed at which we want it to move in the desired direction.
Next, with the "this.transform.Translate()" function, we ensure access to the object to which the script is attached, and with the "transform.Translate()" command, we specify the movements that will be made on the x, y, and z axes.
The operations are performed as Translate(x, y, z), where x, y, and z specify the coordinates here.
With "Input.gyro.rotationRateUnbiased.x * speed * Time.deltaTime" we state that the movement will be along the x plane
With "Input.gyro.rotationRateUnbiased.y * speed * Time.deltaTime" we state that the movement will be along the y plane.
With Time.deltaTime we ensure that the movements are not affected by the update frequency in the update function so that it covers the same distance each second.
Using the Gyroscope in Landscape Mode
When you want to use the gyroscope in landscape mode, you need to swap the x and y axes.
float speed = 1f;
void Update(){
Input.gyro.enabled = true;
this.transform.Translate(Input.gyro.rotationRateUnbiased.y * speed * Time.deltaTime, Input.gyro.rotationRateUnbiased.x * speed * Time.deltaTime, 0);
}If you do not make this change, when the screen is in landscape mode, the object moves in the wrong direction. When you move the device right or left, the object goes forward or backward instead of going right or left..
Visual Explanation
Let's put an object in our scene.
Then let's create a C# file for the codes that will make our object move.
Let's assign the C# file we created to our object.
Now let's write our codes.
This post is prepared to explain it to you basically. If you really want to have the objects move on the map with the gyroscope, you need to use methods like rigidbody.addforce instead of transform.Translate.
Phone Screen Recording
Note: When using the gyroscope, either set the screen to landscape or portrait and lock the screen; otherwise, the user may encounter issues like the screen constantly rotating while using the gyroscope.


Yorum Gönder