0
votes

I m using an orthographic camera in my web Project and this is the code that let me move the camera:

 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); }

now i m trying to convert the project to Android and i m changing the script of the camera , i touch the dvce screen and i can move the camera according to the movement of my finger but when i want to limit the camera movement in left, right , up and down side like the web player version, it's not working ,when i add these conditions [Camera.main.transform.position.y < CameraYLimitUp] , [Camera.main.transform.position.y > CameraYLimitDown] , [Camera.main.transform.position.x > CameraXLimitLeft] , [Camera.main.transform.position.x < CameraXLimitRight] ,here is the code :

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

private float CameraYLimitUp = 18;
private float CameraYLimitDown = -5f;
private float CameraXLimitRight = 21;
private float CameraXLimitLeft = -21;

   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 && Camera.main.transform.position.x < CameraXLimitRight)
        {
            transform.Translate(Vector3.right * Time.deltaTime * 25);
        }
        else if (deltaX > 0 && Camera.main.transform.position.x > CameraXLimitLeft)

            transform.Translate(Vector3.left * Time.deltaTime * 25);
    }
    else{ 
        if (deltaY < 0 && Camera.main.transform.position.y < CameraYLimitUp)
            transform.Translate(Vector3.up * Time.deltaTime * 25);
        else if (deltaY > 0 && Camera.main.transform.position.y > CameraYLimitDown )
            transform.Translate(Vector3.down * Time.deltaTime * 25);
    }
    }

Thanks for your help

1

1 Answers

0
votes

Don't use Camera.main. Instead make variable public GameObject MyCamera and pass camera GameObject. I remember that Camera.main sometomes has problems on android.