Hi guys and gals, I’m making a 2d sidescroller to touch devices and I’m currently stuck with the camera movement.
The camera starts at the given position, but the problem is that when I click on the screen, the camera goes off the scene and far to the right. So I have to drag me back (the camera) to the scene while I hold the mouse button down. When I release the button it stays in position - but when I click again to scroll the same thing happens again (the camera goes far to the right and I have to drag me back to the scene).
To prevent that the camera shall go off the scene, I have a “leftBorder” and a “rightBorder” where the camera not shall pass and only move in between these borders (aka the scene). The problem #2 is that the if statement is somehow ignored.
What am I doing wrong (or not doing at all)?
// erge
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
public bool isMoving = true;
private float camX;
private float camY = 38.0f;
private float camZ = -74.0f;
private float camStartPos = -58.7f;
private float currentCamPos = Camera.main.transform.position.x;
private float leftBorder = -79.0f;
private float rightBorder = 87.0f;
void Start () {
Camera.main.transform.position = new Vector3(camStartPos, camY, camZ);
}
void Update () {
// TODO: Move camera while currentCamPos is between the borders
if(currentCamPos >= leftBorder && currentCamPos <= rightBorder){
if(Input.GetMouseButton(0)){
isMoving = true;
transform.position = new Vector3(camX = Input.mousePosition.x, camY, camZ);
Debug.Log("Mouse X Pos: " + Input.mousePosition.x);
}
else{
Debug.Log("Mouse Not Pressed");
}
}
else{
// TODO: Disable camera movement
isMoving = false;
}
}
}