Hello everyone !
I have just started fiddling with Unity - and saw a lot of questions and methods around Isometric camera views, but none that addresses the fine points of what I’m trying to do.
I’m developping my game with an orthogonal isometric view using the basic approach: my camera object (“IsoCamera”) is rotated according to the required isometric values, and orthogonal. It is inside an empty object (“IsoCameraView”), and i’m manipulating that object to achieve the camera fonctionnality I desire.
Here are their respective setups in the inspector:
As you can see, I’m using a single script called “CameraController”, attached to the IsoCameraView object.
Currently, the script works for panning up/down and left/right (screen-wise, not isometric view-wise) using the arrow keys, zooming in and out using the mouse wheel, and rotating the camera view along an axis that is roughly in the center of the screen.
My objective is to have the camera rotations “smoothed” when the corresponding keys (A and Q) are pressed, pretty much like it appears in this video:
But I can't figure out how to do it.Here is my script (my last attempt is commented out under “//Smoothing”):
using UnityEngine;
using System.Collections;
// Camera Controller Isometric
// Allows the camera to move left, right along a fixed axis, zoom in and out, rotate, tilt
// Attach to a camera GameObject (e.g IsoCamera) for functionality.
public class CameraController : MonoBehaviour {
//Include the CameraObject
public Camera IsoCamera;
// How fast the camera moves (panning)
public int cameraVelocity = 10;
// How fast the camera zooms (zooming)
public float cameraZoomStep = 2f;
// how much the camera rotates (rotating)
float cameraRotationStep = 90f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
KeyboardControl();
MouseControl ();
}
// Smooth Rotating
/*void RotateObject(Vector3 vector3Point, Vector3 vector3Axis, float rotateAmount, float rotateTime) {
float step = 0.0f; //non-smoothed
float rate = 1.0f/rotateTime; //amount to increase non-smooth step by
float smoothStep = 0.0f; //smooth step this time
float lastStep = 0.0f; //smooth step last time
while(step < 1.0f) { // until we're done
Debug.Log ("I've stepped!");
step += Time.deltaTime * rate; //increase the step
smoothStep = Mathf.SmoothStep(0.0f, 1.0f, step); //get the smooth step
transform.RotateAround(vector3Point, vector3Axis, rotateAmount * (smoothStep - lastStep));
lastStep = smoothStep; //store the smooth step
}
//finish any left-over
if(step > 1.0f) {
transform.RotateAround(vector3Point, vector3Axis, rotateAmount * (1.0 - lastStep));
}
}*/
// Keyboard input controls
void KeyboardControl(){
// Left (screen-wise)
if((Input.GetKey(KeyCode.LeftArrow)))
{
transform.Translate((Vector3.left * cameraVelocity) * Time.deltaTime);
transform.Translate((Vector3.up * 0.4f * cameraVelocity) * Time.deltaTime);
}
// Right (screen-wise)
if((Input.GetKey(KeyCode.RightArrow)))
{
transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
transform.Translate((Vector3.down * 0.4f * cameraVelocity) * Time.deltaTime);
}
// Up
if((Input.GetKey(KeyCode.UpArrow)))
{
transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
}
// Down
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
}
// rotate left one step when key pressed
if (Input.GetKeyDown(KeyCode.A)){
transform.RotateAround(this.transform.position, Vector3.up, cameraRotationStep);
}
//rotate right one step when key pressed
if (Input.GetKeyDown(KeyCode.E)){
transform.RotateAround(this.transform.position, Vector3.down, cameraRotationStep);
}
}
//Mouse input controls
void MouseControl(){
//Zooming
//First, adjust Zoom Step depending on current Zoom level
if (IsoCamera.orthographicSize < 8.5f){
cameraZoomStep = 1f;
} else if (IsoCamera.orthographicSize < 20f){
cameraZoomStep = 5f;
} else if (IsoCamera.orthographicSize < 50f){
cameraZoomStep = 10f;
} else {
cameraZoomStep = 20f;
}
//Zoom when mouse wheel used
if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
IsoCamera.orthographicSize = Mathf.Clamp(IsoCamera.orthographicSize+cameraZoomStep, 1f, 50f);
}
if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
IsoCamera.orthographicSize = Mathf.Clamp(IsoCamera.orthographicSize-cameraZoomStep, 1f, 50f);
}
}
}
Thanks a ton to anyone who can help me doing this!