Dragging Camera with Right Mouse Click

Hello,

I am trying to make a camera controller script with JavaScript. Everything but the camera translations are working. I am trying to drag camera with right click.

So far I tried locating mouse X and Z coordinates and reversing the results from camera position but didn’t work. I tried rays as well but couldn’t even compile it with errors. Can anyone help me?

Just in case, the rest of the code might help someone, including camera rotation, zoom and resetting position

function Update ()
{
	//var speed : float = 1.5f;

// Translate Right Click
       if(Input.GetMouseButton(1)) 
       {
 
       }

// Rotate Right Click 
//	if(Input.GetMouseButton(1))
//	    transform.eulerAngles.y += Input.GetAxis("Mouse X") * speed;
	    
//Zoom Out
	if (Input.GetAxis("Mouse ScrollWheel") <0)
	{
		if (Camera.main.fieldOfView<=100)
			Camera.main.fieldOfView +=2;
		if (Camera.main.orthographicSize<=20)
			Camera.main.orthographicSize +=0.5;
	}

//Zoom In
	if (Input.GetAxis("Mouse ScrollWheel") > 0)
	{
		if (Camera.main.fieldOfView>2)
			Camera.main.fieldOfView -=2;
		if (Camera.main.orthographicSize>=1)
			Camera.main.orthographicSize -=0.5;
	}

// Reset Camera Position
	if (Input.GetKeyUp(KeyCode.C))
	{
		Camera.main.fieldOfView = 50;
		//Camera.main.orthographicSize =5;
		//transform.eulerAngles.y = 90;
	}

//EOF				
}

Check my modification of the camera script in this demo here:

http://virtualplayground.d2.pl/Unity/bbox/

It actually rotates on the right drag and moves on the left drag.
If the rest is fine you can just swap the buttons in the script.
It is just my modification of other script taken from somewhere in the net.
There are plenty of scripts like that.
This one is contained inside my free package on the AssetStore here:

Thanks, it will definetely help me!

If it really helps, just give me some stars after downloading on the AssetStore.:smile:

I already favorited your script and will give all stars as well when I finish downloading new version of Unity :smile: Seriously, thanks for sharing! It seems that I have to get used to C# soon :slight_smile:

Hey there! I finally managed to download your script, it is working flawless :slight_smile:

I have a few questions tough,

  1. I am using a modular tile-based system where I don’t have a plane floor to attach camera (and dragging won’t work without it) Is there another way? I can make it working with a plane, but will be trouble when creating random dungeons.

  2. I cannot change the drag action to middle click. I did however make it hold middle click and drag with left mouse. It seems like
    if (Input.GetMouseButtonDown(2)) currentInputPosition = desiredInputPosition; is causing a problem. Is there even middle mouse buttondown action implemented?

Thank you so much for your time!

Great.
1.) Here is a place for changes:

			RaycastHit[] hits;
			//Vector3 hitPoint = Vector3.zero;
			hits = Physics.RaycastAll(camera.ScreenPointToRay(currentInputPosition), 100);	
			var hl = hits.Length;
			//if(hl==0) return;
			
			bool prevDrag = dragging;
			
			dragging = false;
			
			foreach (RaycastHit hit in hits) {
				if(hit.transform.gameObject == terrainMesh){//!!!!!!!!!!
					hitPoint = hit.point;
					dragging = true;
					break;
				}
			}

Basically you have to change the terrain condition.
Yo have to make it detect your particular terrain, - by name or tag or anything.

The other thing is you could just use hit, not hits, if you wouldn’t mind the terrain being hidden by colliders above that. It would just be a bit simpler.

  1. Try to replace the the button number in all the places - unless you want to use all three buttons 0,1,2.

Thanks! At least I know how to approach tiles :slight_smile:
About the drag part, I missed the eventhandler on the bottom of the page, my bad…

Thanks again!