运动Android Unity

时间:2014-07-24 10:30:44

标签: android unity3d

我正在尝试将项目从Web转换为Android。我使用带箭头键的相机,我试图触摸屏幕并向上和向下左右。

这是我在Web上的代码:

if (Input.GetKey(KeyCode.UpArrow) && Camera.main.transform.position.y < CameraYLimitUp)//Up Camera Mvt
        { transform.Translate(-Vector3.up * Time.deltaTime * 20); }

       if (Input.GetKey(KeyCode.DownArrow) && Camera.main.transform.position.y > CameraYLimitDown)//Down Camera Mvt
        { transform.Translate(Vector3.up * Time.deltaTime * 20); }

        if (Input.GetKey(KeyCode.LeftArrow) && Camera.main.transform.position.x > CameraXLimitLeft)//Left Camera Mvt
        { transform.Translate(Vector3.right * Time.deltaTime * 20); }

        if (Input.GetKey(KeyCode.RightArrow) && Camera.main.transform.position.x < CameraXLimitRight)//Right Camera Mvt
        { transform.Translate(Vector3.left * Time.deltaTime * 20); }

感谢您的帮助

/////////////////////////////////////////////// ////////////////////////////////////////                                                  的更新

public float CameraYLimitUp;
public float CameraYLimitDown;
public float CameraXLimitLeft;
public float CameraXLimitRight;
public Vector2 startPos;
public Vector2 endPos;
public bool fingerHold = false;
public bool ButtonPressed = false;

// Use this for initialization
void Start () {
CameraYLimitUp = -1f;
CameraYLimitDown = 1f;
CameraXLimitLeft = 1f;
CameraXLimitRight = -1f;
}

// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.UpArrow) && transform.position.y > CameraYLimitUp)
{ transform.Translate(Vector3.down * Time.deltaTime * 20); }

if (Input.GetKey(KeyCode.DownArrow) && transform.position.y < CameraYLimitDown)
{ transform.Translate(Vector3.up * Time.deltaTime * 20); }

if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x < CameraXLimitLeft)
{ transform.Translate(Vector3.right * Time.deltaTime * 20); }

if (Input.GetKey(KeyCode.RightArrow) && transform.position.x > CameraXLimitRight)
{ transform.Translate(Vector3.left * Time.deltaTime * 20); }

if(!ButtonPressed)
{
if(Input.touchCount > 0){                      
    Touch touch = Input.GetTouch(0);
    if(touch.phase == TouchPhase.Began){       
        startPos = touch.position;             
        fingerHold = true;
    }
    else if(touch.phase == TouchPhase.Moved){  
        endPos = touch.position;               
    }
    else if(touch.phase == TouchPhase.Ended){  
        fingerHold = false;                    
    }
}

if(fingerHold){

    float deltaX = endPos.x - startPos.x;      
    float deltaY = endPos.y - startPos.y;      
    bool horizontal = false;

    if(Mathf.Abs (deltaX) > Mathf.Abs (deltaY)) 
        horizontal = true;                      

    if(horizontal){
        if(deltaX < 0 && transform.position.x < CameraXLimitLeft)
            transform.Translate(Vector3.right * Time.deltaTime * 10);
        else if(deltaX > 0 && transform.position.x > CameraXLimitRight)
            transform.Translate(Vector3.left * Time.deltaTime * 10);
    }
    else{ 
        if(deltaY < 0 && transform.position.y < CameraYLimitDown)
            transform.Translate(Vector3.up * Time.deltaTime * 10);
        else if(deltaY > 0 && transform.position.y > CameraYLimitUp)
            transform.Translate(Vector3.down * Time.deltaTime * 10);
    }
}
}
}
void OnGUI()
{
 if (GUI.Button(new Rect(10, 10, 158, 54), "Click Button"))
    {
       ButtonPressed = true; 
       Print("Button Clicked");
    }
 }

2 个答案:

答案 0 :(得分:0)

您无法在Android中使用箭头键,而是需要模拟像模拟棒这样的东西。此脚本模拟Android中的简单模拟棒:

public Vector2 startPos;
public Vector2 endPos;
public bool fingerHold = false;

void Update () {
    if(Input.touchCount > 0){                      // get total touches on screen
        Touch touch = Input.GetTouch(0);
        if(touch.phase == TouchPhase.Began){       // when finger starts touching
            startPos = touch.position;             // get its position
            fingerHold = true;
        }
        else if(touch.phase == TouchPhase.Moved){  // when finger moves
            endPos = touch.position;               // get its position too
        }
        else if(touch.phase == TouchPhase.Ended){  // when finger released
            fingerHold = false;                    
        }
    }
    if(fingerHold){

        float deltaX = endPos.x - startPos.x;      // get the difference of x position
        float deltaY = endPos.y - startPos.y;      // and the y position
        bool horizontal = false;

        if(Mathf.Abs (deltaX) > Mathf.Abs (deltaY)) // if difference of x is bigger than y
            horizontal = true;                      // meaning we want it to move horizontally

        if(horizontal){
            if(deltaX > 0) // if difference of x is bigger than 0, meaning finger is moving right, translate it to right
                transform.Translate(Vector3.right * Time.deltaTime);
            else if(deltaX < 0) // the reverse, move left
                transform.Translate(Vector3.left * Time.deltaTime);
        }
        else{ // and so on
            if(deltaY > 0)
                transform.Translate(Vector3.up * Time.deltaTime);
            else if(deltaY < 0)
                transform.Translate(Vector3.down * Time.deltaTime);
        }
    }
}

您可以将此脚本与您的脚本合并,因为网络没有触摸,因此此脚本不会与箭头脚本冲突。

我的工作代码:

public float CameraYLimitUp;
public float CameraYLimitDown;
public float CameraXLimitLeft;
public float CameraXLimitRight;
public Vector2 startPos;
public Vector2 endPos;
public bool fingerHold = false;

// Use this for initialization
void Start () {
    CameraYLimitUp = -1f;
    CameraYLimitDown = 1f;
    CameraXLimitLeft = 1f;
    CameraXLimitRight = -1f;
}

// Update is called once per frame
void Update () {
    if (Input.GetKey(KeyCode.UpArrow) && transform.position.y > CameraYLimitUp)
    { transform.Translate(Vector3.down * Time.deltaTime * 20); }

    if (Input.GetKey(KeyCode.DownArrow) && transform.position.y < CameraYLimitDown)
    { transform.Translate(Vector3.up * Time.deltaTime * 20); }

    if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x < CameraXLimitLeft)
    { transform.Translate(Vector3.right * Time.deltaTime * 20); }

    if (Input.GetKey(KeyCode.RightArrow) && transform.position.x > CameraXLimitRight)
    { transform.Translate(Vector3.left * Time.deltaTime * 20); }

    if(Input.touchCount > 0){                      
        Touch touch = Input.GetTouch(0);
        if(touch.phase == TouchPhase.Began){       
            startPos = touch.position;             
            fingerHold = true;
        }
        else if(touch.phase == TouchPhase.Moved){  
            endPos = touch.position;               
        }
        else if(touch.phase == TouchPhase.Ended){  
            fingerHold = false;                    
        }
    }

    if(fingerHold){

        float deltaX = endPos.x - startPos.x;      
        float deltaY = endPos.y - startPos.y;      
        bool horizontal = false;

        if(Mathf.Abs (deltaX) > Mathf.Abs (deltaY)) 
            horizontal = true;                      

        if(horizontal){
            if(deltaX < 0 && transform.position.x < CameraXLimitLeft)
                transform.Translate(Vector3.right * Time.deltaTime * 10);
            else if(deltaX > 0 && transform.position.x > CameraXLimitRight)
                transform.Translate(Vector3.left * Time.deltaTime * 10);
        }
        else{ 
            if(deltaY < 0 && transform.position.y < CameraYLimitDown)
                transform.Translate(Vector3.up * Time.deltaTime * 10);
            else if(deltaY > 0 && transform.position.y > CameraYLimitUp)
                transform.Translate(Vector3.down * Time.deltaTime * 10);
        }
    }
}

尝试与你的比较

答案 1 :(得分:0)

您需要查看移动控件,只需导入Standart Assests(Mobile)*包并查看第一和第三人称控制器预制件,当然还可以查看包中的角色控制器脚本。从那里开始,这一切都非常简单。

*(资产 - &gt;导入包 - &gt;标准资产(移动))

Credit goes here