- Home /
So i need help with the gyroscope in an fps controller
So i have been trying to get the gyroscope working with limiting the rotation of the camera, it worked fine but then the calibration stopped working. i am kinda a beginner and i am trying to get the to work so if anyone could help me i would be more than happy!
here's a copy of the code :
using UnityEngine; using System.Collections;
// Activate head tracking using the gyroscope public class gyroscope : MonoBehaviour { public GameObject player; // First Person Controller parent node public GameObject head; // First Person Controller camera
// The initials orientation
private float initialOrientationX;
private float initialOrientationY;
Quaternion origin = Quaternion.identity;
public float xMinLimit = -360.0f;
public float xMaxLimit = 360.0f;
public float xSensitivity = 100.0f;
public float ySensitivity = 100.0f;
float yRot = 0.0f;
float xRot = 0.0f;
// Use this for initialization
void Start()
{
// Activate the gyroscope
Input.gyro.enabled = true;
// Save the firsts values
initialOrientationX = Input.gyro.rotationRateUnbiased.x;
initialOrientationY = Input.gyro.rotationRateUnbiased.y;
origin = Input.gyro.attitude;
}
// Update is called once per frame
void Update()
{
rotateCamerax();
rotateCameray();
// Rotate the player and head using the gyroscope rotation rate
player.transform.Rotate(0, initialOrientationY-Input.gyro.rotationRateUnbiased.y, 0);
head.transform.Rotate(initialOrientationX-Input.gyro.rotationRateUnbiased.x, 0,0);
}
public void CalibrateYAngle()
{
player.transform.localRotation = Quaternion.Inverse(origin) * Input.gyro.attitude;
head.transform.localRotation = Quaternion.Inverse(origin) * Input.gyro.attitude;
}
void rotateCamerax()
{
yRot += Input.gyro.rotationRateUnbiased.y * ySensitivity*Time.deltaTime;
yRot = Mathf.Clamp(yRot, -90, 90);
player.transform.localEulerAngles = new Vector3(0, -yRot, 0);
}
void rotateCameray()
{
xRot += Input.gyro.rotationRateUnbiased.x * xSensitivity * Time.deltaTime;
xRot = Mathf.Clamp(xRot, -90, 90);
head.transform.localEulerAngles = new Vector3(-xRot, 0, 0);
}
}
BU$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$P
BU$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$P
Your answer
Follow this Question
Related Questions
Why the control with gyroscope inverted when I make 180 degrees turn? 1 Answer
Why is the Gyroscope inverted? 0 Answers
Rotating a camera using the gyroscope 4 Answers
How to set child position and rotation fixed respect parent? 0 Answers
How to move camera position to player object's direction while having looking at object? 0 Answers