- Home /
Make my object's rotation "upright"
I'm making an android game with acceleration controls, and it works well so far but I'd like the rotation of the object to slowly upright itself while still moving in the current direction, while the player isn't using any of the acceleration controls outside of a threshold. I've tried a lot of different things,but nothing is quite the effect I'm looking for. Any help would be appreciated! void Update () {
float phoneTurn = Input.acceleration.x;
float phoneUpDown = Input.acceleration.z;
test = -phoneUpDown;
testTwo = phoneTurn;
transform.Translate(Vector3.forward * Time.deltaTime*5);
if ((phoneUpDown / 2) > .2) {
transform.Rotate(Vector3.left * Time.deltaTime*80);
}
if ((phoneUpDown / 2) < -.2) {
transform.Rotate(Vector3.right * Time.deltaTime*100);
}
if((phoneTurn/2) > .2){
transform.Rotate(Vector3.up * Time.deltaTime*100);
}
if((phoneTurn/2) < -.2){
transform.Rotate(Vector3.down * Time.deltaTime*100);
}
else {
transform.localRotation = Quaternion.Slerp(transform.localRotation, ???, Time.deltaTime * 2.3f);
}
}
Answer by robertbu · Oct 30, 2014 at 07:42 PM
Execute every frame in update:
Quaternion q = Quaternion.FromToRotation(transform.up, vector3.up) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
'speed' is a variable you define. Initialize it with a value of 3.5f to start.
Thanks! Works like a charm! Would have never got it it on my own, never thought to multiply by transform.rotation.
Note that the '*' is a combine not a multiply and is not commutative:
http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html
Alright that makes sense, interesting, thanks for taking the time to let me know that.
Your answer
Follow this Question
Related Questions
accelerometer Rotation limiting 1 Answer
Android Z Axis 0 Answers
Input.gyro.attitude not working on newer Android devices 3 Answers
Tilt object acceloremeter limited 0 Answers
Smooth Follow Camera Rotate on Z-Axis? 2 Answers