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
}
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:
I already favorited your script and will give all stars as well when I finish downloading new version of Unity Seriously, thanks for sharing! It seems that I have to get used to C# soon
Hey there! I finally managed to download your script, it is working flawless
I have a few questions tough,
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.
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?
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.
Try to replace the the button number in all the places - unless you want to use all three buttons 0,1,2.