unity3d - unity 3d rotate the gameobject according to accelerometer -


i want make game temple run. need rotate platform of player according how tilt device. trying accelerometer not game object tilted. please guide me. thanks

this code using code in comments im trying use code out of comments

public class tilt : monobehaviour { vector3 dir; float movespeed; bool istilt; // use initialization void start () {     dir=vector3.zero;     movespeed=10.0f;     istilt=true;  }  // update called once per frame void fixedupdate() {     //dir.x=-input.acceleration.y;     //transform.translate(dir.x,0,0);     //transform.translate(input.acceleration.x, 0, -input.acceleration.z);     //vector3 vec = physics.gravity;     /*vec.x = -physics.gravity.z;     vec.z = physics.gravity.x;     vec.y = 0;     transform.rotation = quaternion.euler(vec);*/     /*movespeed=-input.acceleration.y;      transform.rotate(vector3.up*movespeed*5.0f);  */float zmovment = input.getaxis("horizontal");//-input.acceleration.y;     float xmovment = input.getaxis("vertical");//-input.acceleration.x;     vector3 dir = new vector3(xmovment,0,zmovment);      vector3 tgravity =new vector3(zmovment*2.0f,-15,xmovment*2.0f);     physics.gravity= tgravity;//new vector3(zmovment*speed,gravity,xmovment*speed);     vector3 vec = physics.gravity;     vec.x = -physics.gravity.z;     vec.z = physics.gravity.x;     vec.y = 0;     transform.rotation = quaternion.euler(vec); } 

}

one way of doing use gravity normal of platform this

transform.rotation = quaternion.lookrotation(input.acceleration, vector3.up); 

you might have switch axis on acceleration down forward depending on forward on platform. guess

vector3 forward = new vector3(-input.acceleration.y, input.acceleration.x, input.acceleration.z); transform.rotation = quaternion.lookrotation(forward, vector3.up); 

also note correct when device still. movement side side cause rotation on platform might undesirable.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -