Android设备从各个位置倾斜

时间:2014-04-25 08:58:33

标签: camera unity3d

我正在Unity开发2D狙击游戏。这是一个非常简单的游戏,涉及相机和飞机(背景图像)。

我想通过双击设备让相机在狙击手和普通模式之间切换。在正常模式下,相机将是静态的。所以,根本没有动静。在狙击模式下,摄像机根据设备位置在x和y轴上平移。我确实达到了这个效果(但不正确)。

问题:相机应该从用户握住设备的位置倾斜。目前它仅在平面位置工作。

public Texture2D sniperTexture; // Sniper Texture
public Material  sniperMat; // Sniper Material

public   bool  sniperMode = false; // Sniper Mode Boolean
private  bool  isplaying  = false; // Sniper and Tilt Switch

public  float maxX = 0.0f;
public  float minX = 0.0f;
public  float maxY = 0.0f;
public  float minY = 0.0f;

public  float speed = 6.0f; // Speed for Current Acceleration
public  float filter= 5.0f; // Filter Value for Tilt Function


private Vector3 curAc; // Current Acceleration
private Vector3 zeroAc; // Zero Acceleration
private Vector3 tempAcc; // temp Acceleration

public Vector3  normCamPos;


void Start()
{
    sniperMode = false; // Sniper Mode Toggle
    ResetAxes(); // Reset Axis for Tilt Function in Zoom Mode/ It works from any position depending on the user.
}   


void OnPostRender() // During Post Render Sniper Texture is been displayed
{
    if(sniperMode)
    Graphics.Blit( sniperTexture,null,sniperMat); // Sniper Texture
}


void FixedUpdate()                                                                              
{
    DoubleTap();
    gyroMovement();
}


void DoubleTap()        
{
    for (var i=0; i< Input.touchCount; ++i)
    {
        if(Input.GetTouch(i).phase == TouchPhase.Began)
        {
            if(Input.GetTouch(i).tapCount == 2)
            {
                sniperMode=!sniperMode;
                Camera.main.fieldOfView = sniperMode ? 20 : 60;
                isplaying = true;                          
            }               
        }
        else
        {
               isplaying = false;                       
        }           
    }
}


void gyroMovement() // Tilt Movement
{
    if(sniperMode=sniperMode)
    {   
        tempAcc = Vector3.Lerp(curAc,Input.acceleration-zeroAc,filter*Time.deltaTime);                    // Vecor 3 values are stored in CurrentAccelration variable.

        Vector3 dir = new Vector3(tempAcc.x,tempAcc.y,0);                                             

        if(dir.sqrMagnitude > 1)           

        dir.Normalize();

        transform.Translate(dir*speed*Time.deltaTime);
        transform.Translate(dir*speed*Time.deltaTime);          

        var pos = transform.position;

        pos.x = Mathf.Clamp(pos.x, minX,maxX);
        pos.y = Mathf.Clamp(pos.y, minY,maxY);

        transform.position = pos;
    }
    else
    {
        transform.position = normCamPos;
        isplaying = false;
    }
}

void ResetAxes() // Reset Axis Function for tilt.
{
    zeroAc = Input.acceleration;
    curAc =  Vector3.zero;
}

0 个答案:

没有答案