- Home /
How to rotate around the centre of screen on the Y axis in othrographic
I asked this in another post but could not post anymore on that page so I've started a new topic!
I am trying to rotate on Y around the centre of the screen in othographic mode. I do not want any tilt whatsoever (I'm going for a 3d isometric theme). The problem is that the rotation goes in a large circular arch and the centre screen is lost until the rotation is complete.
I was given advice to make the script holding parent of the camera the centre of the camera viewpoint from the get go. This worked initially but as soon as I moved the parent, the camera would go on these large circular rotations and the original centre of the screen would be lost from sight.
I've attached the code I'm using. It's cobbled together from various tutorials and has some of my own additions. My camera is in orthographic, its positioned so the parent is in the centre, the script is attached to the parent, and near clipping is set to -100.
Hope that is enough info! Ideally I would love to find a way for the camera to stop blurring when it moves.... but maybe I shouldn't get greedy in this post ;)
using UnityEngine;
using System.Collections;
public class IsometricCamera : MonoBehaviour
{
//camera move speed
public int cameraSpeed;
// Update is called once per frame
void Update ()
{
//translating on the x, z and Y axes using WASD
if(Input.GetKey("w"))
{
transform.Translate(Vector3.up * cameraSpeed * Time.deltaTime );
}
if(Input.GetKey("s"))
{
transform.Translate(Vector3.down * cameraSpeed * Time.deltaTime );
}
if(Input.GetKey("d"))
{
transform.Translate((Vector3.right + Vector3.back) * cameraSpeed * Time.deltaTime);
}
if(Input.GetKey("a"))
{
transform.Translate((Vector3.left + Vector3.forward) * cameraSpeed * Time.deltaTime );
}
//zooming up and down with the scrollWheel
GameObject isoView = GameObject.Find("Main Camera");
if(Input.GetAxis ("Mouse ScrollWheel") > 0 && isoView.camera.orthographicSize > 2)
{
isoView.camera.orthographicSize = isoView.camera.orthographicSize - 1;
}
if(Input.GetAxis ("Mouse ScrollWheel") < 0 && isoView.camera.orthographicSize < 20)
{
isoView.camera.orthographicSize = isoView.camera.orthographicSize + 1;
}
//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);
}
}
}
Answer by sevensixtytwo · Sep 13, 2014 at 02:46 PM
Hmmm. What kind of isometric camera are you working on? I'm unsure of what your code would do since you have
transform.Translate((Vector3.right + Vector3.back) * cameraSpeed * Time.deltaTime);
...which makes it odd that you also have Vector3.up and Vector3.down there. Unless we're talking non-Euclidean geometries here. xD
Try this. It's derived from a top-down isometric camera I did for school.
if(Input.GetKey("w"))
{
transform.Translate(transform.forward * cameraSpeed * Time.deltaTime );
}
if(Input.GetKey("s"))
{
transform.Translate(-transform.forward * cameraSpeed * Time.deltaTime );
}
if(Input.GetKey("d"))
{
transform.Translate(transform.right * cameraSpeed * Time.deltaTime);
}
if(Input.GetKey("a"))
{
transform.Translate(-transform.right * cameraSpeed * Time.deltaTime );
}
if(Input.GetKey("e"))
{
transform.Rotate(transform.up * cameraSpeed * Time.deltaTime);
}
if(Input.GetKey ("q"))
{
transform.Rotate(-transform.up * cameraSpeed * Time.deltaTime);
}
Put it on an empty gameObject at position (0,0,0) and rotation (0,0,0). Then parent a camera to it with position (-18.371,15,-18.371) and rotation (30,45,0).
It should look like a Diablo 1/2 style camera and moves relative to its own rotation. Even if you rotate it, it will move in the local forward.
Thanks a lot for your answer. You fixed my rotation problem but the style of movement is something I tried very hard to change before. When I move up I want the camera to go directly up (not north west), and when I go right or left, I want it to go perfectly east and west respectively in relation to the screen view. This is why I have that peculiar line of code (that gives me crappy rotation). It's the only way I found to make sure that the camera pans properly. Also, when I do rotate it, I want s to still take me down in relation to the screen and not up etc. Using your code gave me great rotation but all the controls effectively become switched when I do rotate.
The camera view I'm trying to emulate is found in games like Timber and Stone and Anno 1404. Any ideas?
I see. So the camera should go directly north, south, east and west in relation to the camera, yes? Not sure about your problem with the controls switching though. Tell me more about it.
In the meantime, try setting the parent to rotations (0,45,0) and the camera's transforms to positions (-2.109775e-06,15,-25.98076) and rotations (30,0,0). See how it works.
Not having much luck. The script I had first is still the only one that moves as I want it to move, but your one is still the only one that rotates in the desired fashion -even though if I rotate the parent then the WASD keys move the parent according to it's position and not the viewpoint of the screen. I hope that made sense. I am very new to vectors!!!
Thanks so much for the suggestions by the way! Is there any way I can send you a basic unity file with the scene, code, camera etc? I'm not sure if pas$$anonymous$$ll can take something like that.
That'd be great. You can pack the most relevant files in a unitypackage. I'm guessing a scene with a few primitives and one script wouldn't be too big to attach to a comment or something.
hey sorry for the late reply! I'm not getting notified for some reason (sometimes it does notify me but it seems to be quite sporadic!).
I'm having a bit of trouble uploading a package to unity answers. It says the file type is not valid. Do you need unity pro to successfully export a package? I followed the steps on how to make a package and it seemed pretty straight forward. The best I can do is this link to my good drive:
https://drive.google.com/file/d/0B4OAxw1eGEZ4Q0ZOcGR$$anonymous$$cDFHNWc/edit?usp=sharing
hope it works!
Your answer
Follow this Question
Related Questions
Orthographic Camera rotation on world y-axis but also rotates z-axis slightly? 0 Answers
Using a perspective camera can an object act as if viewed by orthographic camera? 1 Answer
How to freeze the rotation or the position on the camera transform? 1 Answer
how do i make first person character rotate left and right along with camera? 0 Answers