Search Unity

Standard Assets - RTS Camera

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

  1. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    Does Unity have any standard asset RTS cameras I can drag and drop?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome!

    Is there even such a thing as a standard RTS camera? Most RTS games I've played have had similar camera systems, but the specifics are different enough that I don't know how a standard asset would capture all cases.

    The Standard Assets includes a Cameras package that has several preset camera types you can drop in, but honestly, I think you're better off just creating your own. Scrolling and zooming the camera are fairly trivial things, especially when compared to the rest of what would be involved in creating an RTS.

    If you haven't already, it's highly recommended you go through the Official Tutorials to learn the ins and outs of creating with Unity.
     
  3. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    I don't recall if the Standard Assets have one, but for most RTS games you want to have what's called an Orbit camera.
     
  4. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    An Orbit Camera, thanks.

    A lot of people say a first person camera is trivial, but there is a first person shooter camera ready to drag and drop.

    It'd be much better to have an RTS camera released by Unity Team than me making it myself.

    I've been doing the tutorials, they are indeed very helpful.
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It's the age-old debate around here. Which things should Unity include, and which things should it empower devs to create for themselves? Or the third alternative (which I forgot to mention before), the Asset Store! Which just so happens to have a few RTS cameras available.
     
  6. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    Thanks!
     
  7. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    btw the beginning of Chapter 7 in my book Unity in Action goes over how to program an orbit camera. Obviously you wouldn't buy an entire book for just one thing, but the book covers a lot of other useful stuff too.
     
  8. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    Programming an RTS Camera has been a nightmare. It's extremely difficult. I've done so many tutorial and every RTS Camera has flaws.

    I guess Unity can't handle such a simple thing, how sad. Does this mean Unity is only good for FPS? Because that is the only Camera that seems to work properly.

    I did this tutorial. The problem with this one is you have to pan with the left mouse button which is really stupid. You can't just use the WASD keys.


    Then I tried this one.
    - The problem with this one is the height adjustment doesn't work on objects like Cube's or Spheres that are in the scene even if they have collider and rigidbody.

    I wish Unity had complete solutions for basic things, then I could focus on the actual game instead of messing around with a camera for days. *kicks Unity*

    I am now going to spend a third day wasting my time making a stupid generic RTS camera that should've been a part of Standard Assets.

    OH LOOK AT THIS ONE -
    He says for learning purposes this tutorial is great. DUDE I DON'T WANT TO LEARN ANYTHING, I WANT TO MAKE A GOD DAMN GAME.

    Look, another incomplete RTS Camera -


     
    Last edited: Oct 24, 2015
  9. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    It's a god damn miracle, I finally found a camera that does what I want. The guy who wrote this and shared is awesome.

    Try his online demo of this RTS Camera here - http://www.windsoftrade.net/stratecam/

    Source Code - https://github.com/federicocasares/stratecam

    This is the kind of RTS Camera that should be a Standard Asset of Unity.

    To show I'm not some ungrateful bastard, I'm going to convert his code from JS to C#, get it working in my project then paste the source code here giving full credit to him.
     
    Last edited: Oct 24, 2015
  10. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It is a poor craftsman who blames his tools...

    Unity is perfectly capable of handling any type of camera system. What is a camera type, anyway, but a system of rules for how it behaves? You process input, manipulate it's position, rotation, zoom, etc., and place restrictions to keep it contained to the boundaries you want. Since Unity provides you with simple means of doing all those things, any failure to meet those terms is not on Unity's part.

    I'd also argue that exactly because there are code samples out there like the one you indicated by Mr. Casares is exactly why Unity shouldn't include it as a standard asset. It's already available to you for free. All you had to do was put a little bit of work into finding it.

    With this statement alone, I don't believe you have what it takes to actually complete a game. I don't know how anyone could expect to do something they don't know how to do without learning how to do that thing first...

    I dunno, maybe I'm wrong. Maybe you'll make a great game and be super successful by only copying and pasting other people's work together.
     
    TakuanDaikon and Hadryel like this.
  11. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    There is one HUGE topic missing from this: https://unity3d.com/learn/tutorials/ - CAMERAS

    https://unity3d.com/learn/tutorials/Cameras/WASD_Movement/
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RTSCamera : MonoBehaviour
    6. {
    7.  
    8.   //
    9.  
    10.  
    11.   //
    12.  
    13.   public Terrain terrain;
    14.  
    15.   public float panSpeed = 15.0f;
    16.   public float zoomSpeed = 100.0f;
    17.   public float rotationSpeed = 50.0f;
    18.   public float mousePanMultiplier = 0.1f;
    19.   public float mouseRotationMultiplier = 0.2f;
    20.   public float mouseZoomMultiplier = 5.0f;
    21.   public float minZoomDistance = 20.0f;
    22.   public float maxZoomDistance = 200.0f;
    23.   public float smoothingFactor = 0.1f;
    24.   public float goToSpeed = 0.1f;
    25.   public bool useKeyboardInput = true;
    26.   public bool useMouseInput = true;
    27.   public bool adaptToTerrainHeight = true;
    28.   public bool increaseSpeedWhenZoomedOut = true;
    29.   public bool correctZoomingOutRatio = true;
    30.   public bool smoothing = true;
    31.  
    32.   public GameObject objectToFollow;
    33.   public Vector3 cameraTarget;
    34.  
    35.   private float currentCameraDistance;
    36.   private Vector3 lastMousePos;
    37.   private Vector3 lastPanSpeed = Vector3.zero;
    38.   private Vector3 goingToCameraTarget = Vector3.zero;
    39.   private bool doingAutoMovement = false;
    40.  
    41.   void Start()
    42.   {
    43.   currentCameraDistance = minZoomDistance + ((maxZoomDistance - minZoomDistance) / 2.0f);
    44.   lastMousePos = Vector3.zero;
    45.   }
    46.  
    47.   void Update()
    48.   {
    49.   UpdatePanning();
    50.   UpdateZooming();
    51.   UpdatePosition();
    52.   UpdateAutoMovement();
    53.   lastMousePos = Input.mousePosition;
    54.  
    55.   }
    56.  
    57.  
    58.  
    59.   void GoTo(Vector3 position)
    60.   {
    61.   doingAutoMovement = true;
    62.   goingToCameraTarget = position;
    63.   objectToFollow = null;
    64.   }
    65.  
    66.   void Follow(GameObject gameObjectToFollow)
    67.   {
    68.   objectToFollow = gameObjectToFollow;
    69.   }
    70.  
    71.   private void UpdatePanning()
    72.   {
    73.   Vector3 moveVector = new Vector3(0, 0, 0);
    74.   if (useKeyboardInput)
    75.   {
    76.   if (Input.GetKey(KeyCode.A))
    77.   {
    78.   moveVector += new Vector3(-1, 0, 0);
    79.   }
    80.   if (Input.GetKey(KeyCode.S))
    81.   {
    82.   moveVector += new Vector3(0, 0, -1);
    83.   }
    84.   if (Input.GetKey(KeyCode.D))
    85.   {
    86.   moveVector += new Vector3(1, 0, 0);
    87.   }
    88.   if (Input.GetKey(KeyCode.W))
    89.   {
    90.   moveVector += new Vector3(0, 0, 1);
    91.   }
    92.   }
    93.  
    94.   if (useMouseInput)
    95.   {
    96.   if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftShift))
    97.   {
    98.   Vector3 deltaMousePos = (Input.mousePosition - lastMousePos);
    99.   moveVector += new Vector3(-deltaMousePos.x, 0, -deltaMousePos.y) * mousePanMultiplier;
    100.   }
    101.   }
    102.  
    103.   if (moveVector != Vector3.zero)
    104.   {
    105.   objectToFollow = null;
    106.   doingAutoMovement = false;
    107.   }
    108.  
    109.   var effectivePanSpeed = moveVector;
    110.   if (smoothing)
    111.   {
    112.   effectivePanSpeed = Vector3.Lerp(lastPanSpeed, moveVector, smoothingFactor);
    113.   lastPanSpeed = effectivePanSpeed;
    114.   }
    115.  
    116.   float oldRotation = transform.localEulerAngles.x;
    117.  
    118.   Vector3 _tmp = transform.localEulerAngles;
    119.   _tmp.x = 0.0f;
    120.   transform.localEulerAngles = _tmp;
    121.  
    122.   float panMultiplier = increaseSpeedWhenZoomedOut ? (Mathf.Sqrt(currentCameraDistance)) : 1.0f;
    123.   cameraTarget = cameraTarget + transform.TransformDirection(effectivePanSpeed) * panSpeed * panMultiplier * Time.deltaTime;
    124.  
    125.   Vector3 _tmp1 = transform.localEulerAngles;
    126.   _tmp1.x = oldRotation;
    127.   transform.localEulerAngles = _tmp1;
    128.   }
    129.  
    130.   private void UpdateRotation()
    131.   {
    132.  
    133.   if (useKeyboardInput)
    134.   {
    135.   if (Input.GetKey(KeyCode.Q))
    136.   {
    137.   }
    138.   if (Input.GetKey(KeyCode.E))
    139.   {
    140.  
    141.   }
    142.   }
    143.  
    144.   if (useMouseInput)
    145.   {
    146.   if (Input.GetMouseButton(2))
    147.   {
    148.   }
    149.   }
    150.   }
    151.  
    152.   private void UpdateZooming()
    153.   {
    154.   float deltaZoom = 0.0f;
    155.   if (useKeyboardInput)
    156.   {
    157.   if (Input.GetKey(KeyCode.F))
    158.   {
    159.   deltaZoom = 1.0f;
    160.   }
    161.   if (Input.GetKey(KeyCode.R))
    162.   {
    163.   deltaZoom = -1.0f;
    164.   }
    165.   }
    166.   if (useMouseInput)
    167.   {
    168.   float scroll = Input.GetAxis("Mouse ScrollWheel");
    169.   deltaZoom -= scroll * mouseZoomMultiplier;
    170.   }
    171.   float zoomedOutRatio = correctZoomingOutRatio ? (currentCameraDistance - minZoomDistance) / (maxZoomDistance - minZoomDistance) : 0.0f;
    172.   currentCameraDistance = Mathf.Max(minZoomDistance, Mathf.Min(maxZoomDistance, currentCameraDistance + deltaZoom * Time.deltaTime * zoomSpeed * (zoomedOutRatio * 2.0f + 1.0f)));
    173.   }
    174.  
    175.   private void UpdatePosition()
    176.   {
    177.   if (objectToFollow != null)
    178.   {
    179.   cameraTarget = Vector3.Lerp(cameraTarget, objectToFollow.transform.position, goToSpeed);
    180.   }
    181.  
    182.   transform.position = cameraTarget;
    183.   transform.Translate(Vector3.back * currentCameraDistance);
    184.  
    185.   if (adaptToTerrainHeight && terrain != null)
    186.   {
    187.   Vector3 _tmp2 = transform.position;
    188.   _tmp2.y = Mathf.Max(terrain.SampleHeight(transform.position) + terrain.transform.position.y + 10.0f, transform.position.y);
    189.   transform.position = _tmp2;
    190.   }
    191.   }
    192.  
    193.   private void UpdateAutoMovement()
    194.   {
    195.   if (doingAutoMovement)
    196.   {
    197.   cameraTarget = Vector3.Lerp(cameraTarget, goingToCameraTarget, goToSpeed);
    198.   if (Vector3.Distance(goingToCameraTarget, cameraTarget) < 1.0f) {
    199.   doingAutoMovement = false;
    200.   }
    201.   }
    202.   }
    203. }
    204.  
    These are what I need now.
    https://unity3d.com/learn/tutorials/Cameras/IntroHowCamerasCanMoveOrbitRotate
    https://unity3d.com/learn/tutorials/Cameras/WASD_Movement/ElevateOverGameObjects
    https://unity3d.com/learn/tutorials/Cameras/QE_Rotate/
    https://unity3d.com/learn/tutorials/Cameras/MiddleMouseButtonFreeLook/
    https://unity3d.com/learn/tutorials/Cameras/ScrollInOutZoom
    https://unity3d.com/learn/tutorials/Cameras/MovementBoundaries


    The problem is piecing all these features together from different cameras, that take's expertise with Unity & Cameras that I don't have, it will take weeks just to learn that. I thought Unity was about RAD.

    The above categories should pretty much cover RTS cameras. I already contributed the WASD movement from https://github.com/federicocasares/stratecam, converted the code from JS to C#.
     
    Last edited: Oct 24, 2015
  12. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Well I'm certainly not going to argue against more tutorials, as having more learning materials is always good.

    The problem I have is that I don't think an RTS camera is any more important than a Grand Strategy style camera, or an Infinite Runner or MMO framework. Or a voxel world generator. Or a Collectible Card Game system. Sure, including any of these things as standard assets would save a lot of developers a lot of time, but because of these facts:
    • There are existing assets in the Asset Store or in free repositories providing these functionalities
    • The variations in specific needs for these systems would make a comprehensive solution cumbersome for many users, while a less feature-rich offering would not be lacking for others.
    • The official and third party tutorials provide a solid foundation of knowledge to build on and create whatever you actually need yourself.
    ... I believe Unity's time is best spent doing things we can't do, like fixing bugs, adding support for new platforms, and improving performance of the engine as a whole.
     
  13. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    InfiniteRunner looks like third person camera, that camera is already a part of Unity Standard Assets.
    MMO Framework camera is also third person camera. that camera is already a part of Unity Standard Assets.
    Grand Strategy needs a WASD movement camera system, I pasted the code for that.

    There are only so many cameras for the various genre's of games. The RTS camera is extremely neglected in Unity's Standard Assets.

    Hopefully I can remedy that. I already posted the WASD movement code for the camera from that one guy's example. It works great, the best I've seen out of all the tutorials. Now I just need to somehow incorporate the other features.

    I'm currently trying to figure out how to zoom a camera in where the camera is facing, then tilt the camera up the more you zoom in, like how it's done in Total War series.
     
  14. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,156
    Exactly! Every camera solution has advantages and disadvantages. This is why Unity often does not provide a solution but rather provides the tools to make your own.
     
    Schneider21 likes this.
  15. sr3d

    sr3d

    Joined:
    Oct 19, 2015
    Posts:
    78
    Awww GOD DAMN IT. The WASD movement camera code is conflicting with the zoom in code. The zoom just won't work. *KICK* I tries to zoom in, but keeps getting sucked back.

    I hate reinventing the wheel, I hate it so much.

    No wonder so many games come out buggy. Game Programming Logic is completely garbage, pure trash. That is why so many games are buggy as hell when they come out. None of this code makes any sense.
     
  16. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    A more realistic reason for any lack of quality would be impatience regarding the learning process.
     
    Ryiah and jhocking like this.