Hello, I have created a camera that can rotate according to the gyrometer on the phone. The camera rotation was working fine, but I am not really sure why the camera is not moving when the position of the Android device changes. I think one of the reasons might be related to the small values from the accelerometer data. However, the camera still does not move even after I amplified the values by adding a movement scale (set to 100f).
using UnityEngine;
public class GyroMovement : MonoBehaviour
{
public float speed = 100.0f; // Base speed
public float movementScale = 10.0f; // Increased scale factor for movement
private Vector3 acceleration;
private Vector3 gravity = Vector3.zero;
private Vector3 deltaAcceleration = Vector3.zero;
private float highPassFilterFactor = 0.1f; // Adjust this value for filtering
private float debugUpdateInterval = 1.0f; // Interval for debug logs
private float nextDebugUpdateTime = 0.0f; // Timer for debug logs
void Start()
{
if (SystemInfo.supportsGyroscope)
{
Input.gyro.enabled = true;
}
}
void Update()
{
// Get the accelerometer data
acceleration = Input.acceleration;
// Apply high-pass filter to remove gravity
gravity = Vector3.Lerp(gravity, acceleration, highPassFilterFactor);
deltaAcceleration = acceleration - gravity;
// Calculate movement direction
Vector3 moveDirection = new Vector3(deltaAcceleration.x, 0, deltaAcceleration.y);
if (moveDirection.sqrMagnitude > 1)
{
moveDirection.Normalize();
}
// Amplify movement direction for more noticeable movement
moveDirection *= speed * movementScale;
// Debug before applying the movement
Debug.Log("Move Direction before applying: " + moveDirection);
Debug.Log("Transform Position before applying: " + transform.position);
// Move the transform.position
transform.position += moveDirection * Time.deltaTime;
// Debug after applying the movement
Debug.Log("Move Direction * Time.deltaTime: " + (moveDirection * Time.deltaTime));
Debug.Log("Transform Position after applying: " + transform.position);
#region Gyroscope rotation
// Gyroscope for rotation
if (Input.gyro.enabled)
{
Quaternion deviceRotation = Input.gyro.attitude;
deviceRotation = new Quaternion(deviceRotation.x, deviceRotation.y, -deviceRotation.z, -deviceRotation.w);
transform.localRotation = deviceRotation;
}
#endregion
if (Time.time >= nextDebugUpdateTime)
{
nextDebugUpdateTime = Time.time + debugUpdateInterval;
Debug.Log("Acceleration: " + acceleration);
Debug.Log("Gravity: " + gravity);
Debug.Log("Delta Acceleration: " + deltaAcceleration);
Debug.Log("Delta Acceleration Magnitude: " + deltaAcceleration.magnitude);
Debug.Log("Move Direction: " + moveDirection);
Debug.Log("Transform Position: " + transform.position);
Debug.Log("Move Direction * Time.deltaTime: " + (moveDirection * Time.deltaTime));
}
}
}