How do I control the camera position (in between borders)?

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

You can’t assign camera position values straight from mouseinput. It’s not the same coordinate system. Well I mean you could if you were always going to have the exact same screen sizes (Screen.height / width) and they perfectly mapped to the area you were covering but that would be absurd, what you really want is a transformation matrix. Try this:

Ray hovray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hovhit =  new RaycastHit();
		if (Physics.Raycast(hovray,out hovhit) ) { 
			wp = hovhit.point;
			wp.y = 0;

With a ground collider this will show where your mouse is intersecting with the terrain. Change the y value to be above the ground to where you want the camera.

Then save the position of the camera between successive updates when you have a mouseup and use that as the offset for the next mousedown. This should solve your problem as I’m doing the exact same thing with a mouseorbit and I had similar ‘jumping’ issues. If this isn’t clear enough say so.

There’s another function also you can check out in API docs: ScreenToWorldPoint here is an example; The z coordinate is distance from the camera projection into the world. So this resolves coordinates 30 units in from of the camera where the mouse is projecting through.

Vector3 temps = Input.mousePosition;
temps.z = 30;
glit.transform.position = Camera.main.ScreenToWorldPoint(temps);

Alternatively if you don’t want to do this, just save the camera offset and mouse offset from mouseup, and then increase the camera’s position by the difference in Input.mousepos values multiplied by a speed constant. Use GetInput for more sensitivity also.