- Home /
Rotating and zooming with an isometric camera
I've made an isometric camera which is controlled by the WASD keys using this tutorial; http://kraigwalker.com/articles/creating-an-isometric-camera-in-unity/
Now I just want to make it so my camera can be rotated clockwise and anticlockwise with the Q & E keys and for the camera to zoom in and out with the Mouse wheel. Can anybody create me a script that would enable these camera movements?
Answer by edcarlo · Jan 07, 2014 at 05:25 AM
int speed = 5;
void Update(){
if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
transform.Translate(Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Q)){
transform.Rotate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E)){
transform.Rotate(Vector3.forward* -speed * Time.deltaTime);
}
}
Should I just add this to the existing script or create an entirely new one (if so, what should I call the script?)
just copy the if else statements then add to your script's Update method.
the scrip does not work for me. The rotate pivots the camera around on the wrong axis (not sure which one but the camera starts moving under the terrain so it's not the right one!). The zoom just moves left and right. This happens in both orthographic and perspective. Any help would be appreciated.
Use transform.forward and transform.back for the zoo$$anonymous$$g, and -transform.right and transform.right for the rotation.
If you want the camera to rotate "around" a virtual center point, then I recommend making it a child of an empty gameobject and moving and rotating that one.
@mattockman - Both $$anonymous$$axW's script on his page as well as these additions by edcarlo, should work just fine. $$anonymous$$ake sure the script is attached to the camera and not some parent GO that you made. That kind of parent arrangement (with child camera having a non-standard orientation) would explain your issue.
Another possibility is using an old version of Unity, or...?
Fairly recently, and I'm not sure when, Unity added more "meanings" to the Rotate method using the Space argument.
For example, for the ccw one ('q"), you can use Vector3's forward combined with the "Self" space:
transform.Rotate(Vector3.forward* speed * Time.deltaTime, Space.Self);
is kind of like saying "rotate around the static Vector3 forward in relation to the local forward direction (see this by clicking on the Editor's Local versus Global button to see.
Another other hand if you rotate around the local space directly, that is transform's forward, then you need to rotation around the "World" space or they can (kind of) cancel each other out. So, another way to do the same is:
transform.Rotate(transform.forward * speed * Time.deltaTime, Space.World);
Hope this helps.
Answer by vadithiyan · Mar 22, 2015 at 11:21 PM
I do not know if you still need help, but in case you do or if anyone is looking for it, you cannot zoom in by moving the camera forward in Isometric mode. You can zoom in and out increasing or decreasing Camera.orthographicSize values in script.
if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
camera.orthographicSize += 1; //Change values according to your requirements
}
if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
camera.orthographicSize -= 1;
}
The higher the orthographicSize the more area the camera can see, so increase it to zoom out and decrease it to zoom in.
Answer by devstudents · Sep 12, 2014 at 08:35 PM
Thanks for the reply Cherno and mcroswell! Sorry for the late reply. I didn't get notified of these reponses!
Thanks for the tip about Space.World. To be honest, I don't understand what it means but attaching it to the code got me closer to what I was after:
//rotate
if(Input.GetKey("e"))
{
transform.Rotate(Vector3.up * cameraSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey ("q"))
{
transform.Rotate(Vector3.down * cameraSpeed * Time.deltaTime, Space.World);
}
I have three questions now though:
I still couldn't get the camera to rotate properly without using the above code. Is this because we were trying achieve different types of rotations? I do not want the camera to tilt in anyway. Just to rotate on the Y axis.
how do you stop the camera from the blurring the terrain when it moves?
if anyone can go into more detail about how to rotate the camera around a central point I will be very grateful.
As I said before, you just parent the camera to an empty object, set the camera's position and rotation so that the parent is in the center of the viewport, and the camera at the right height. Then, all the code concerning camera movement can be applies to the parent ins$$anonymous$$d.