Let Translate only work on Z-axis

Hi guys,

I’ve made this script for a flycam which I’m gonna be using in my strategy/building game.

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

	bool isPanning = false;
	Vector3 mouseOrigin;
	public float panSpeed = 3.0f;

	void Update() {
		if (Input.GetMouseButtonDown (0)) {
			isPanning = true;
			mouseOrigin = Input.mousePosition;
		}

		if (!Input.GetMouseButton (0))
			isPanning = false;

		if (isPanning) {

			Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
			Vector3 move = new Vector3(pos.x * panSpeed, 0, pos.y * panSpeed);
			transform.Translate(move, Space.World);
		}
	}
}

The problem now is that, because I’ve also rotated the camera on the X-axis, the camera will hit the ground after a while. How can I make sure that the camera only follows the rotation on the Z-axis?

Kind Regards,

Julian

Use Rigid Body Constraints.
You can attach rigidbody component to the camera and deselect use gravity followed by Freezing the rotation on X and Y Axis.42950-capture.jpg