Search Unity

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

Discussion in 'Getting Started' started by sr3d, Oct 25, 2015.

  1. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    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.
     
    Last edited: Oct 29, 2015
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    sr3d likes this.
  3. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    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.
     
    Last edited: Oct 26, 2015
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    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:
    Code (CSharp):
    1.  
    2. Vector3 tempPosition = transform.position;
    3. float tempY = tempPosition.y;
    4. tempPosition += direction * Time.deltaTime * camMoveSpeed;
    5. tempPosition.x = Mathf.Clamp(tempPosition.x, etc
    6. tempPosition.z = Mathf.Clamp(tempPosition.z, etc
    7. tempPosition.y = tempY;
    8. transform.position = tempPosition;
    9.  
     
    Last edited: Oct 26, 2015
    sr3d likes this.
  5. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    Nice! I updated the original code with your optimizations :)


    Code (csharp):
    1.  
    2. void moveCameraXZ(Vector3 direction)
    3. {
    4. Vector3 calcMovPos = transform.position;
    5. float tempY = calcMovPos.y;
    6.  
    7. if (Input.GetKey(KeyCode.LeftShift))
    8. {
    9. calcMovPos += (direction * Time.deltaTime * (camMoveSpeed + camShiftMoveSpeedModifier));
    10. }
    11. else
    12. {
    13. calcMovPos += (direction * Time.deltaTime * camMoveSpeed);
    14. }
    15.  
    16. calcMovPos.x = Mathf.Clamp(calcMovPos.x, terrainBoundaryLimitX, terrainWidth - terrainBoundaryLimitX);
    17. calcMovPos.z = Mathf.Clamp(calcMovPos.z, terrainBoundaryLimitZ, terrainLength - terrainBoundaryLimitZ);
    18. calcMovPos.y = tempY;
    19.  
    20. transform.position = calcMovPos;
    21. }
    22.  
    Like you said, read it, modify it, set it. :)
     
    Last edited: Oct 26, 2015
    jhocking likes this.
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    tiny thing I just noticed: your comment //store the original Y position should be restore not store
     
  7. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    fixed, thx!
     
  8. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    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 - http://forum.unity3d.com/threads/ho...er-what-direction-youre-facing-solved.364279/