• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by e-bonneville · Mar 21, 2010 at 02:28 AM · camerazoomrtsscrolling

RTS Style Camera Scrolling

I've decided to make a RTS (real time strategy) game. I have a camera set up at the angle I want, and everything is coming along fine. However, I need some method of moving the camera. I suppose I could use the arrow keys, but I'd really rather not. So, deciding to make a camera scroll where you simply move the mouse to the edge of the screen and have the camera scroll in that direction, I started looking around in order to locate something that could help me out. I spent about three hours surfing, and came up with... nothing! So, I thought I'd just ask another question for everybody that's already given up in despair. How would you implement such a feature?

Thanks in advance - Elliot Bonneville

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tecfix · Jan 18, 2015 at 09:20 PM 0
Share

I know this is an old thread but I found it very useful and it still works! Thanks

9 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MattyWS · Jan 24, 2013 at 12:30 AM

Hello, I was messing with RTS cameras and used some of the code here, My camera stops after a certain distance from the center, has zoom using the mouse wheel and also has keyboard controls for arrow keys to use the camera.

On top of these features, zooming really far out no longer slows the cameras movement or zooming (when zoomed out, trying to scroll in any direction or zoom back in was really slow).

Also! And this ones a big one~ the camera moves in world space, so all you need to do is move the camera how high you want it and what angle you want it (could be isometric) and it wont move into the terrain. I //commented some things so people dont get lost. Code below is in C#

   using UnityEngine;
     using System.Collections;
     
     public class RTSCamera : MonoBehaviour
     {
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     
         float mousePosX = Input.mousePosition.x; 
         float mousePosY = Input.mousePosition.y; 
         int scrollDistance = 5; 
         float scrollSpeed = 2 * Camera.main.orthographicSize + 2;
         Vector3 aPosition = new Vector3(0, 0, 0);
         float ScrollAmount = scrollSpeed *Time.deltaTime;
         const float orthographicSizeMin = 15f;
         const float orthographicSizeMax = 256f;
             
     
             //mouse left
         if ((mousePosX < scrollDistance) && (transform.position.x > -240))
              { 
               transform.Translate (-ScrollAmount,0,0, Space.World); 
              } 
             //mouse right
         if ((mousePosX >= Screen.width - scrollDistance) && (transform.position.x < 240))
              { 
                transform.Translate (ScrollAmount,0,0, Space.World);  
              }
             //mouse down
         if ((mousePosY < scrollDistance) && (transform.position.z > -240))
              { 
               transform.Translate (0,0,-ScrollAmount, Space.World); 
              } 
             //mouse up
         if ((mousePosY >= Screen.height - scrollDistance) && (transform.position.z < 240))
              { 
               transform.Translate (0,0,ScrollAmount, Space.World); ; 
              }
             //Keyboard controls 
         if ((Input.GetKey(KeyCode.UpArrow)) && (transform.position.z < 240))
              { 
               transform.Translate (0,0,ScrollAmount, Space.World); ; 
              } 
         if ((Input.GetKey(KeyCode.DownArrow)) && (transform.position.z > -240))
              { 
               transform.Translate (0,0,-ScrollAmount, Space.World);
              }
         if ((Input.GetKey(KeyCode.LeftArrow)) && (transform.position.x > -240))
              { 
               transform.Translate (-ScrollAmount,0,0, Space.World);  
              } 
         if ((Input.GetKey(KeyCode.RightArrow)) && (transform.position.x < 240))
              { 
               transform.Translate (ScrollAmount,0,0, Space.World); 
              }
             //Scrolling Zoom
           if (Input.GetAxis("Mouse ScrollWheel") < -0) // forward
                 {
            Camera.main.orthographicSize *= 1.1f;
                 }
         if (Input.GetAxis("Mouse ScrollWheel") > -0) // back
             {
            Camera.main.orthographicSize *= 0.9f;
                 }
     
         Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, orthographicSizeMin, orthographicSizeMax );
     }
     }
 
  
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by smirlianos · Apr 12, 2013 at 05:18 PM

It's an old post but I will answer anyways!!! You can use this script I bought and I use. It's pretty good and costs only 3€ for both commercial and indie use.

RTS Camera Script

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Teh great Lizard King · Jul 29, 2014 at 08:26 PM

Here's a script I came up with:

 var speed = 3;     //MUST BE INTEGER!!!
 var size = 100;    //cursor to edge tweaker
 
 function Update () {
 
     var mousePos = Input.mousePosition;
        
     if(mousePos.x <= size)
     transform.position += new Vector3(-speed,0,0);
 
     if(mousePos.x >= Screen.width - size)
     transform.position += new Vector3(speed,0,0);
 
     if(mousePos.y <= size)
     transform.position += new Vector3(0,0,-speed);
 
     if(mousePos.y >= Screen.height - size)
     transform.position += new Vector3(0,0,speed);

     if (Input.GetAxis("Mouse ScrollWheel") < 0)
     transform.position += new Vector3(0,speed,-speed);   
   
     if (Input.GetAxis("Mouse ScrollWheel") > 0)
     transform.position += new Vector3(0,-speed,speed);    
 }

I know the thread is from the stone age, but I felt like I should post here.

Should be easy to understand and is short in line numbers, hope this helps anybody trying to make a rts game camera

g

od, had to edit about 10000 times X.X

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Tsilliev · Jul 04, 2017 at 11:03 AM

Thank you the great lizard king, I improved it more, because yours doesn't move in the direction the camera is facing if you rotate. I edited your script so:

  1. Camera pans, meaning you can look around by holding right mouse button

  2. You can enable disable screen edge scrolling

  3. You can use WASD

  4. The camera moves where it is looking but its Y position is locked and can be changed only by "zooming" or scrolling

To setup, you:

  1. attached this script to camera

  2. create EmptyObject (name it however you like), the object should NOT be child of camera

  3. attach the object to the script, what it does is that it copies the Y rotation of the camera, so you can actually go forward and backwards but on the same Y position.

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class CameraMovement : MonoBehaviour {

        public int speed = 3;     //MUST BE INTEGER!!!
           public int sizeEdge = 100;    //adjusts the size of how big the trigger cursor area is
           public bool ScreenScrolling = false; //enable disable edge screen scrolling
           Vector3 lastMousePosition;
           public float rotateSpeed = 10;
           
           bool wKeyDown = false;
           bool aKeyDown = false;
           bool sKeyDown = false;
           bool dKeyDown = false;
           bool qKeyDown = false;
           bool eKeyDown = false;
           
           public GameObject EmptyObject; //create empty object outside camera, this object has zero X,Z rotation so our camera doesnt fly up/down 
         
           
         // Use this for initialization
         void Start () {
         
         }
         
         // Update is called once per frame
          
         
     
      
         void Update () 
         {
           
           wKeyDown = Input.GetKey(KeyCode.W); // buttons
           aKeyDown = Input.GetKey(KeyCode.A);
           sKeyDown = Input.GetKey(KeyCode.S);
           dKeyDown = Input.GetKey(KeyCode.D);
           qKeyDown = Input.GetKey(KeyCode.Q);
           eKeyDown = Input.GetKey(KeyCode.E);
          
          Vector3 mousePos = Input.mousePosition;
             
          if(mousePos.x <= sizeEdge && ScreenScrolling || aKeyDown && !dKeyDown) // move left
          transform.position += -transform.right;
      
          if(mousePos.x >= Screen.width - sizeEdge && ScreenScrolling || dKeyDown && !aKeyDown) // move right
          transform.position += transform.right;
      
          if(mousePos.y <= sizeEdge && ScreenScrolling || sKeyDown && !wKeyDown) // move backward by using the empty object's zero X,Z rotation so we cannot move up/down
          transform.position += EmptyObject.transform.forward * -1;
          
          if(mousePos.y >= Screen.height - sizeEdge && ScreenScrolling || wKeyDown && !sKeyDown) // move forward by using the empty object's zero X,Z rotation so we cannot move up/down
          transform.position += EmptyObject.transform.forward;
         
         
          
          if (Input.GetAxis("Mouse ScrollWheel") < 0 ) // move up/down by "zooming"
          transform.position += new Vector3(0,speed,0);   
        
          if (Input.GetAxis("Mouse ScrollWheel") > 0)
          transform.position += new Vector3(0,-speed,0);   
         
         if (Input.GetMouseButton(1)) // panning/looking around by holding right mouse button
                 {
                     // if the game window is separate from the editor window and the editor
                     // window is active then you go to right-click on the game window the
                     // rotation jumps if  we don't ignore the mouseDelta for that frame.
                     Vector3 mouseDelta;
                     if (lastMousePosition.x >= 0 &&
                         lastMousePosition.y >= 0 &&
                         lastMousePosition.x <= Screen.width &&
                         lastMousePosition.y <= Screen.height)
                         mouseDelta = Input.mousePosition - lastMousePosition;
                     else
                         mouseDelta = Vector3.zero;
      
                     var rotation = Vector3.up * Time.deltaTime * rotateSpeed * mouseDelta.x;
                     rotation += Vector3.left * Time.deltaTime * rotateSpeed * mouseDelta.y;
                     transform.Rotate(rotation, Space.Self);
      
                     // Make sure z rotation stays locked
                     rotation = transform.rotation.eulerAngles;
                     rotation.z = 0;
                     transform.rotation = Quaternion.Euler(rotation);
                 }
                     lastMousePosition = Input.mousePosition;
     
             EmptyObject.transform.localEulerAngles = new Vector3(0.0f,transform.localEulerAngles.y, 0.0f); 
         }
      
          
     }
     
    
    
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Zoom problem 1 Answer

Fixing Issues with custom RTS Camera Controll Script 1 Answer

RTS Camera Zoom w/ Rotation 0 Answers

Attaching an RTS scrolling script to a camera (Noob Question) 3 Answers

Understanding Gun Scopes & Fov 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges