- Home /
unity horizontal and vertical axis based on gyro?
Hey. i want to do a tilting platform thing using the gyroscope of my phone. at the moment it looks like this using WASD keys: https://dribbble.com/shots/2205018-iMaze-Tilting-Behavior
what i want to do now is that the platform gets tilted (horizontal and vertical input)based on how you tilt your ipad or phone. the code looks like this at the moment:
using UnityEngine;
using System.Collections;
public class TiltPlatform : MonoBehaviour {
public bool smoothMotion = true;
//This is how quickly we MoveTowards the input axis.
public float smoothSpeed = 1f;
//The maximum we want our input axis to reach. Setting this lower will rotate the platform less, and higher will be more.
public float multiplier = 0.1f;
//Variables, don't edit these.
private float hSmooth = 0f;
private float vSmooth = 0f;
private float h;
private float v;
void Update () {
//Get Vertical and Horizontal axis from Input
h = Input.GetAxis("Horizontal")*multiplier;
v = Input.GetAxis("Vertical")*multiplier;
}
void FixedUpdate() {
hSmooth = Mathf.MoveTowards(hSmooth,h,smoothSpeed* Time.deltaTime);
vSmooth = Mathf.MoveTowards(vSmooth,v,smoothSpeed * Time.deltaTime);
//Rotate to match the new axis using EulerAngles.
if(!smoothMotion){
transform.rotation = Quaternion.EulerAngles(new Vector3(h,0f,v));
}else{
transform.rotation = Quaternion.EulerAngles(new Vector3(hSmooth,0f,vSmooth));
}
}
}
Answer by tanoshimi · Nov 18, 2015 at 01:17 PM
Ok, and what's the problem you're facing? Unity comes with a tilt control as part of the UnityStandardAssets.CrossPlatformInput - just drop it into your project.
ah, cool. didnt know that. just drop the $$anonymous$$obileTiltControlRig prefab inside the scene where the script above is and thats it?
The prefab will have a script on it, check out the contents if you want to re-purpose the method.
ok, got it. just had to change this lines:
//Get Vertical and Horizontal axis from Input
//h = Input.GetAxis("Horizontal")*multiplier;
//v = Input.GetAxis("Vertical")*multiplier;
h = Input.acceleration.x;
v = Input.acceleration.y;
Answer by stf940 · Feb 04, 2019 at 09:05 PM
i put this on my game but is to slow . how i can incrase sensivity?