Is there a way to get the direction of the gyro ? If it’s possible i want to use gyro as input and move my first person controller based on some of the gyro axes.
My idea was to create a GUI Texture and when the user is touching the GUI to enable movement. My problems :
-
if i take the input from accelerometer after i touch the GUI i must get my phone parallel to the ground and after the touch ends my gyro is facing down.
-
if i rotate 180 degrees after i moved and want to walk again, my accelerometer is backwards (moving forward is moving back now )
Any idea on how to move my first person controller in my scene based on gyro is welcomed.
I use this code to walk in my scene :
function Start(){
gyro = Input.gyro;
}
function FixedUpdate() {
if (grounded) {
moveDirection = new Vector3 (gyro.gravity.y, 0,gyro.gravity.x);
gameObject.Find(“print”).guiText.text = moveDirection.ToString();
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;if (walk){
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
}
function Update() {
if (Input.touchCount > 0){
var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began && arrow.HitTest(touch.position)){
walk = true;
}
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
walk = false;
}
}
}