我在网络项目中使用了正交相机,这是让我移动相机的代码:
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); }
现在我试图将项目转换为Android并且即时更改相机的脚本,我触摸dvce屏幕,我可以根据手指的移动移动相机,但是当我想限制左边的相机移动时,当我添加这些条件时,正确,上下都像网络播放器版本,它不起作用[Camera.main.transform.position.y&lt; CameraYLimitUp],[Camera.main.transform.position.y&gt; CameraYLimitDown],[Camera.main.transform.position.x&gt; CameraXLimitLeft],[Camera.main.transform.position.x&lt; CameraXLimitRight],这是代码:
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);
}
}
感谢您的帮助
答案 0 :(得分:0)
不要使用Camera.main。而是使变量公共GameObject MyCamera并传递相机GameObject。我记得Camera.main sometomes在android上有问题。