- Home /
3D RTS camera
Hi! I've been struggeling with a problem now for at least 5 days. I need a camera that should ALWAYS stay on the same Y-axis but it should be able to move on the X and Z-axis. It should also be rotateable horizontally and a bit vertically (say 60 degrees maybe). It would essentialy be like an RTS camera but 3D and rotatable. I'd also like it to have some zoom. How would I make this? Logic and/or scripts are appreciated! :)
Thanks!
Answer by Cherno · Oct 05, 2013 at 09:43 PM
I had to create something similar for an isometric game, here is what I did:
I created an empty gameobject as a "Camera Helper", then made the Camera a child of it, and set the camera's transform to my liking (position from parent, rotation etc.). Just make sure that the camera's viewport center is pointing directly at the helper object.
The camera helper is set at whatever position you wish, and if want your camera to move, you instead just move and roate the helper object (along the x and z axes, if you wish). This means you get to use things like transform.forward which is very helpful.
Note that for rotations around the y axis (sideways), you should rotate the helper, and for the x axis (up&down), the camera itself.
Here's some camera move an zoom code. The script is attached to the camera.
You can ignore everything about the orthographic mode if you camera is perspective, and vice versa. I just liked to have the option of changing it during runtime for specific camera shots.
var CameraMover : GameObject;
var orthZoomStep : float= 10.0f;
var orthZoomMaxSize : int = 500;
var orthZoomMinSize : int = 300;
var orthographicView : boolean;//set to true if the Camera is set to orthographic.
function Start()
{
if(GetComponent(Camera).orthographic == true)
{
orthographicView = true;
}
}
function Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
CameraMover.transform.Translate(Vector3.forward * ScrollSpeed);
}
if(Input.GetKey(KeyCode.DownArrow))
{
CameraMover.transform.Translate(-Vector3.forward * ScrollSpeed);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
CameraMover.transform.Translate(-Vector3.right * ScrollSpeed);
}
if(Input.GetKey(KeyCode.RightArrow))
{
CameraMover.transform.Translate(Vector3.right * ScrollSpeed);
}
if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow))
{
CameraMover.transform.Translate((Vector3.forward + (-Vector3.right)) * ScrollSpeed);
}
if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
{
CameraMover.transform.Translate((Vector3.forward + Vector3.right) * ScrollSpeed);
}
if(Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))
{
CameraMover.transform.Translate((-Vector3.forward + (-Vector3.right)) * ScrollSpeed);
}
if(Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))
{
CameraMover.transform.Translate((-Vector3.forward + Vector3.right) * ScrollSpeed);
}
zoomCamera();
}
function zoomCamera()
{
// zoom out
if (Input.GetAxis("Mouse ScrollWheel") < 0)//Mouse Wheel Down
{
if(orthographicView)
{
if (camera.orthographicSize <=orthZoomMaxSize)
{
camera.orthographicSize += orthZoomStep;
}
}
else
{
if (camera.fieldOfView<=150)
{
camera.fieldOfView +=5;
}
}
}
// zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)//Mouse Wheel Up
{
if(orthographicView)
{
if (camera.orthographicSize >= orthZoomMinSize)
{
camera.orthographicSize -= orthZoomStep;
}
}
else
{
if (camera.fieldOfView>2)
{
camera.fieldOfView -=5;
}
}
}
Answer by meat5000 · Oct 05, 2013 at 08:22 PM
Don't know if this is a good method or not;
Add a rigidbody to the camera and mark it as Kinematic. In the rigidbody you can then set Constraints to freeze movement and rotation in the three global axes. Kinematic will mean that it will move only under script control.
Answer by Elowan · Oct 07, 2013 at 12:17 PM
There is One Thing missing for a really perfect 3D RTS Cam: Zoom and center the Cam on a Gameobject, when click let us say left mouse & ALT. If clicked, the Cam First center smooth to the gameobject and if clicked again, a smooth zoom (by distance, not by FOV) is performed.
Any ideas on how to implement this here?
Your answer
Follow this Question
Related Questions
Limiting vertical camera rotation 0 Answers
Camera Movement and angles 2 Answers
Move relative to screen 0 Answers
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
RTS Mouse Click Movement 1 Answer