2013-07-03 41 views
0

我想创建倾斜效果就像寺庙run.I有一个角色控制器(玩家)在游戏中正在通过controller.for移动向前移动(transform.forward)之后,我适用倾斜给它精益IT左右。此前的倾斜我试图通过直接使用transform.translate /tranform.position通过加速度计读数这样的修改玩家的位置:倾斜控制类似寺庙运行

mytransform.translate(acceleration.x*Time.delaTime*5.0f); 

但有一个问题当我晃动设备时,我的相机开始猛拉,播放器也随后使用以下代码在正Z轴上创建倾斜

Vector3 dir = new Vector3(accel.x*8f, 0,0); 
    if (dir.sqrMagnitude > 1) 
{ 
    dir.Normalize(); 

} 
dir.x = Mathf.Round(dir.x * 10f)/10f; 

//mytemp is used for temp storage of player position added with the acceleration 
mytemp = mytransform.position+(mytransform.right*dir.x*Time.deltaTime*5.0f); 

Vector3 diffVec=Vector3.zero; 
///position of the element on which the player is colliding; 
Vector3 col_pos=Collidingelement.transform.position; 
Vector3 unitvec=new Vector3(1,0,0); 
//removing x and z of the collider 
diffVec= Vector3.Scale(col_pos,unitvec); 
//nullify the y,z of the updated mytransform position so that the distance is only measured on X 
Vector3 ppos = Vector3.Scale(mytemp,unitvec); 
//calculate the distance between player & colliding element 
disti=Vector3.Distance(ppos,diffVec); 
disti=Mathf.Clamp(disti,-1.5f,1.5f); 
disti = Mathf.Round(disti * 10f)/10f; 
//update the player position and apply tilt to it if the distance is less than 1.5f 
if(disti<=1.50f) 
{ 
mytransform.position=mytemp; 

} 

现在这有一个问题,如果可以说我在加速度中的值为0.1,则继续更新我的温度,如果距离较小,我的玩家将开始靠在一侧,尽管我将设备放在相同的位置加速度值始终为0.1

回答

0

为什么不计算delta加速度?

bool isMoved = false; 
Vector2 lastAccel = Vector2.Zero; 
Vector2 margin = new Vector2(0.05f, 0.05f); //not processing if its in margin. 
Vector2 deltaAccel = currAccel - lastAccel ; 

if(deltaAccel.x < margin.x) 
{ 
    //don't process 
    isMove = false; 
} 
else 
{ 
    isMove = true; 
    lastAccel = currAccel; 
} 

if(isMove) 
{ 
//Process with deltaAccel here 
}