Can someone explain basic RTS camera theory that takes place in void update () { }

void update () {

  • WASD Movement on Terrain with auto elevation over gameobjects like cubes, spheres and boundaries to stop camera from steering into space.
if input.WASD
transform.position
if hover over object, elevate camera
if boundarydetected stop camera movement
  • QE rotate around wherever camera is facing.
if input.QE
transform.rotatearound(where ever camera is facing)
  • Holding down middle mouse button free look
if input.MiddleMouseButtonDown
camera.freelook();
  • Mouse scroll in and out to zoom in and out
if input.scrolling
  camera.move(forward or back);

}

What camera methods should I use so they don’t conflict with each other?
transform.position ?
transform.rotatearound ?
transform.freelook() ?
transform.localEulerAngles ?? WTF??
transform.transformdirection ??? omg come on already

My current problem is that I can only have one or the other. I don’t know how to combine these functionalities. I have logic that handles zoom, but if I try to mix it with WASD movement, everything breaks.

The camera is no different than any other object. It has a transform; it goes where you tell it to.

So, yeah, if you tell it conflicting things, you probably won’t like the results.

A google search for “RTS camera unity” returns 10 pages of results. Including this asset as the second link, if you’d rather spend $20 than spend several days reinventing the wheel. $20 is a perfectly reasonable price if it saves you even half an hour, let alone several days, but if you can’t do that, consider this open-source one a little further down the first page of results.

But, assuming you want to reinvent the wheel, here’s how to approach it…

Your camera script will keep track of:

  • a point of interest (POI), sometimes called a “focal point”, i.e. the point on the map it’s looking at
  • a (possibly constant) angle up from the ground, say, 45°
  • a distance from the focal point
  • an angle around the Y axis

So, to find the camera position, you start at the POI, rotate a vector around by the Y angle, and up by the ground angle, and then extend it by the distance. That’s your basic camera position.

Then, if you want to maintain a constant (or minimum) distance over the ground, you cast a ray straight down from there. Get the distance to whatever it hits, and adjust the Y position of your camera accordingly. And after you position the camera, you have it LookAt the POI.

Now it should be easy to see how the various controls work: WASD moves the POI on the map (using the camera’s forward/right vectors); Q and E change the Y angle; scroll wheel changes the distance (or alternatively, the camera fieldOfView). I’m not sure what “free look” means, but perhaps it means to temporarily ignore the POI and rotate the camera in its current position directly.

This should get you started if you want to roll it yourself; otherwise, use one of the solutions above, or one of the many others that I’m sure are out there.

And I hope you will soon come to terms with what Unity does and doesn’t include out of the box. It’s not a framework for making any particular genre of game; it’s a development environment. It does not and should not provide things like an RTS-specific camera control, any more than it should have a built-in 2D destructible terrain component. What seems terribly important to you would be useless bloat to 99% of Unity developers, which is why they have a huge asset store for providing exactly those sorts of things, not to mention an enormous community providing blogs, articles, and open-source code libraries. This is all as it should be.

Cheers,

  • Joe
3 Likes

I crashed course cameras and your guide has helped tremendously.

… I want to reiterate how accurately you nailed the theory here.

WASD camera movement with terrain elevation and mouse look rotation is like the core foundation of MANY, MANY RTS style games.

1 Like