Mr. Generic RTS Camera Movement Script v1.01a (FREE)

I’ve moved to the Unreal Engine.

If I’m going to waste time programming custom made cameras and menu systems, then I might as well use a better engine.

You might consider posting this in the Unify wiki for scripts.

1 Like

I updated the script with new features.

  • Camera no longer needs RigidBody (NEW)
  • Auto move forward when holding down right and left click mouse buttons. (NEW)
  • Left Shift to make camera move faster (NEW)
  • Automatic Terrain Boundary checks based on percentage of Terrain’s width/length. (NEW)

Does anyone know a way to avoid calling the transform.position twice in the moveCameraXZ code?

I’ll do that as soon as soon as I do more research. I want to make sure the script isn’t noobish.

Since the first time you call it is an increment (+=) and the last time is setting it (=) then I think you just need both of those separately. You could avoid the increment at the start by storing the position in a separate variable, but then you’re still calling transform.position in order to do that.

The real issue is that you actually call transform.position 5 times, not twice. Note that you save transform.position in the vector limitTerrainMovement, but then keep calling transform.position in the subsequent 2 lines. When programming you can use a variable to do some math that then gets stored back in the same variable. So that would look something like limitTerrainMovement.x = Mathf.Clamp(limitTerrainMovement.x, etc.

Meanwhile, if you put my two suggestions together, you can reduce the 5 references to transform.position down to 2:

Vector3 tempPosition = transform.position;
float tempY = tempPosition.y;
tempPosition += direction * Time.deltaTime * camMoveSpeed;
tempPosition.x = Mathf.Clamp(tempPosition.x, etc
tempPosition.z = Mathf.Clamp(tempPosition.z, etc
tempPosition.y = tempY;
transform.position = tempPosition;
1 Like

Nice! I updated the original code with your optimizations :slight_smile:

void moveCameraXZ(Vector3 direction)
{
Vector3 calcMovPos = transform.position;
float tempY = calcMovPos.y;

if (Input.GetKey(KeyCode.LeftShift))
{
calcMovPos += (direction * Time.deltaTime * (camMoveSpeed + camShiftMoveSpeedModifier));
}
else
{
calcMovPos += (direction * Time.deltaTime * camMoveSpeed);
}

calcMovPos.x = Mathf.Clamp(calcMovPos.x, terrainBoundaryLimitX, terrainWidth - terrainBoundaryLimitX);
calcMovPos.z = Mathf.Clamp(calcMovPos.z, terrainBoundaryLimitZ, terrainLength - terrainBoundaryLimitZ);
calcMovPos.y = tempY;

transform.position = calcMovPos;
}

Like you said, read it, modify it, set it. :slight_smile:

1 Like

tiny thing I just noticed: your comment //store the original Y position should be restore not store

fixed, thx!

Added new feature:

  • C key automatically lowers the camera towards the terrain at specified angle. (NEW)
  • Renamed some variables so they make more sense
  • Ensured proper use of Eulers via use of Quaternions to avoid Gimbal Lock.

If you see code errors or anything that doesn’t make sense, please list it here and it will be fixed asap.

Thanks to Joe Strout, lots of help in this thread - How do you rotate the camera down to 45 degrees no matter what direction you're facing? (SOLVED) - Getting Started - Unity Discussions