How to code Camera Rotation On Drag?

  1. User clicks anywhere and drags left.
  2. Cameras smoothlookat target moves screen right on its x axis only.
  3. Once they drag past a certain point the target stops, so it can’t go beyond a certain position on its x axis.
  4. And same functionality for dragging right, camera rotates to follow target moving screen left.

I have a couple objects with OnMouseDown functions so I’m not sure how they’ll be affected, or if they’ll even get in the way at all if I’m using an OnMouseDrag function. But I can’t figure out how to use one with a collider that is clickable from anywhere on screen without blocking other objects with OnMouseDown functions.

I’ve searched and can only find examples from panning the camera on drag and without any limitations, it’s limiting the movement that’s really got me stumped.

Can anyone explain how I would go about this?

You start with the MouseOrbit script, and add and if(!Input.GetButton(“Fire1”) ) return; to it. This means that when the person clicks and holds it will move, but not until then. :wink:

Ok that makes sense, the MouseOrbit is not really applicable to me here but the SmoothLookAt is and I can use the if(!Input.GetButton(“Fire1”) ) in the same way.

But how would I translate the target on its x axis by clicking and dragging? I mean, how can I convert mouse input to transform input for the target object?

And how would I then limit the target so it can only move between two defined values on it’s x axis? So say it moves between -1 and 1 but cannot move beyond that.

you want the camera to rotate around (orbit) the object while looking at it, only when the mouse button is held down? I still think you’d want to use MouseOrbit.js. You’d just need to clamp the maximum rotation on the X similarly to how they already clamp the rotation on the Y in that script.

Well no maybe I explained myself badly, my camera has a smoothLookAt script on it, when the target of this script moves the camera rotates to look at it smoothly. I want to be able to move that target on drag, so far I’ve accomplished that much but I can’t figure out how to limit the movement.

function Update ()
{
	if(Input.GetMouseButton(0))
	{
		Mathf.Clamp(this.transform.localPosition.x, -1.6, 1.6);
		this.transform.localPosition.x += Input.GetAxis("Mouse X") / 4 * -1;
		print(this.transform.localPosition.x);
	}
}

Also, for some reason the object is not moving on its Local X axis even tho I’ve specifically told it to :confused: