- Home /
input.acceleration - change the zero position of the phone?
Hey Friends, so i have this pice of Code: 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;
h = Input.acceleration.y*multiplier;
v = Input.acceleration.x*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));
}
}
}
which causes this: https://d13yacurqjgara.cloudfront.net/users/76292/screenshots/2205018/tiltplatform.gif
which is great so far, no problem at all. but at the moment i have to lay my phone straight to the horizon so that the platform is not moving in any directions (0°) what i want to achieve is that when you tilt your phone... lets say 45° the platform should be straight. and then when you place it straight to the horizon the platform should tilt back. here is a small sketch for better understanding:
i think the most important line for from the script is this:
void Update () {
//Get Vertical and Horizontal axis from Input
//h = Input.GetAxis("Horizontal")*multiplier;
//v = Input.GetAxis("Vertical")*multiplier;
h = Input.acceleration.y*multiplier;
v = Input.acceleration.x*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));
}
}
I'm not sure if you can change the zeroing out part of the acceleromer but you could just store some floats and apply them every update. Like say you have a screen where the user changes the angle on their phone to be the zero'd out angle. Store those values and apply them each interation to be its "new" zero'd position. Then just apply their movements on top of that? Never tried it but just throwing out ideas.
hm. so actually when
v = Input.acceleration.x*multiplier;
then i just have to say on update vzero = v - 45 (i actually dont know how the tilting float looks like when i hold the phone 45°.. 0.45, 45, 0,0045? and then
vSmooth = $$anonymous$$athf.$$anonymous$$oveTowards(vSmooth,vzero,smoothSpeed * Time.deltaTime);
right?
I don't believe it calculates it in degrees. I'm not entirely sure what value you should put in but you could Debug.Log the base acceleration and then move it into your desired value. Then with the desired values, use them to zero it out. For more of an idea of what I mean, check this out http://answers.unity3d.com/questions/663283/how-to-calibrate-my-accelerometer-data.html . This person obtains the accelerometer values when the script Start()'s and then applies them to calibrate it. FYI - I'm talking about the answer to the question, not the question itself.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to create tilt control similar to temple run? 0 Answers
Detect car acceleration with device motion sensors and GPS 0 Answers
Move if mobile tilts in positive Y direction 0 Answers
Desperate for help on control for a ball on android phone. 1 Answer
iPad: accelerator tilt breaks collider 3 Answers