【问题标题】:Tilt Control Similar to Temple run倾斜控制类似于 Temple run
【发布时间】:2013-07-04 15:38:15
【问题描述】:

我想创建像寺庙奔跑一样的倾斜效果。我在游戏中有一个角色控制器(玩家)通过控制器向前移动。移动(transform.forward)之后我对其应用倾斜以倾斜它左右。之前我尝试通过使用 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

【问题讨论】:

    标签: unity3d accelerometer tilt


    【解决方案1】:

    为什么不直接计算增量加速度。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多