Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
4
Best Answer

Answer by Eric5h5 · Mar 21, 2010 at 04:13 AM

The Update function in the editor script in this example project has code for scrolling. Including the edge of the screen method, arrow keys, and middle mouse button method; pretty much all the RTS games I've played have at least the first two. Not including the public variables for scrolling speed and so on, the relevant code is:

var mPosX = Input.mousePosition.x; var mPosY = Input.mousePosition.y;

// Do camera movement by mouse position if (mPosX < scrollArea) {myTransform.Translate(Vector3.right -scrollSpeed Time.deltaTime);} if (mPosX >= Screen.width-scrollArea) {myTransform.Translate(Vector3.right scrollSpeed Time.deltaTime);} if (mPosY < scrollArea) {myTransform.Translate(Vector3.up -scrollSpeed Time.deltaTime);} if (mPosY >= Screen.height-scrollArea) {myTransform.Translate(Vector3.up scrollSpeed Time.deltaTime);}

// Do camera movement by keyboard myTransform.Translate(Vector3(Input.GetAxis("EditorHorizontal") scrollSpeed Time.deltaTime, Input.GetAxis("EditorVertical") scrollSpeed Time.deltaTime, 0) );

// Do camera movement by holding down option or middle mouse button and then moving mouse if ( (Input.GetKey("left alt") || Input.GetKey("right alt")) || Input.GetMouseButton(2) ) { myTransform.Translate(-Vector3(Input.GetAxis("Mouse X")*dragSpeed, Input.GetAxis("Mouse Y")*dragSpeed, 0) ); }

Comment
Add comment · Show 15 · 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 e-bonneville · Mar 21, 2010 at 03:18 PM 0
Share

Wow! I'm impressed... It worked almost right off the bat. Thanks, Eric!

avatar image e-bonneville · Mar 21, 2010 at 03:20 PM 0
Share

One problem however. Don't know if I want to post this as a new question, but my cam is rotated, and when I go forward or backwards, it goes up slightly, because the rotation is pointing up. I was looking around for a 'lock translation' option, but I thought I'd just ask because your answer was so useful.

avatar image e-bonneville · Mar 21, 2010 at 03:50 PM 0
Share

Never$$anonymous$$d, I got it. Slightly unorthodox solution of placing a collider above and below the camera. Bit jerky every now and then, but it works. Better solution, though, = happy me.

avatar image Eric5h5 · Mar 21, 2010 at 04:20 PM 0
Share

@elbon96: try adding Space.World to the Translate function.

avatar image rocket350 · Sep 16, 2015 at 10:27 AM 1
Share

The scrollArea variable is a integer (int). It describes how close the mouse must be to the edge of the screen for the camera to scroll (in pixels).

Show more comments
avatar image
2

Answer by feillyne · Mar 07, 2012 at 12:14 PM

Modified, working example of above code:

var mousePosX = Input.mousePosition.x; var mousePosY = Input.mousePosition.y; var scrollDistance : int = 5; var scrollSpeed : float = 70;

if (mousePosX < scrollDistance) { transform.Translate(Vector3.right -scrollSpeed Time.deltaTime); } if (mousePosX >= Screen.width - scrollDistance) { transform.Translate(Vector3.right scrollSpeed Time.deltaTime); }

if (mousePosY < scrollDistance) { transform.Translate(Vector3.up -scrollSpeed Time.deltaTime); } if (mousePosY >= Screen.height - scrollDistance) { transform.Translate(Vector3.u) scrollSpeed Time.deltaTime); },var mousePosX = Input.mousePosition.x; var mousePosY = Input.mousePosition.y; var scrollDistance : int = 5; var scrollSpeed : float = 70;

if (mousePosX < scrollDistance) { transform.Translate(Vector3.right -scrollSpeed Time.deltaTime); } if (mousePosX >= Screen.width - scrollDistance) { transform.Translate(Vector3.right scrollSpeed Time.deltaTime); }

if (mousePosY < scrollDistance) { transform.Translate(Vector3.up -scrollSpeed Time.deltaTime); } if (mousePosY >= Screen.height - scrollDistance) { transform.Translate(Vector3.up scrollSpeed Time.deltaTime); }

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
1

Answer by Aggressor · Apr 30, 2012 at 08:21 PM

Thanks Feilyne (though you pasted the code twice in a row).

I am doing an iso metric RTS game (with C#). Because of this the Z axis was moving through the terrain (rather than scrolling up).

So to address this I created an empty game object (which was aligned with the terrain). Attached the camera movement script to it, and then nested the main camera inside the empty object.

Also here is the C# code I used (modified from Feilynes)

using UnityEngine; using System.Collections;

public class CameraController : 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 = 70;

     if (mousePosX < scrollDistance) 
             { 
                 transform.Translate(Vector3.right * -scrollSpeed * Time.deltaTime); 
             } 
             
     if (mousePosX >= Screen.width - scrollDistance) 
             { 
                 transform.Translate(Vector3.right * scrollSpeed * Time.deltaTime); 
             }
     
     if (mousePosY < scrollDistance) 
             { 
                 transform.Translate(transform.forward * -scrollSpeed * Time.deltaTime); 
             } 
             
     if (mousePosY >= Screen.height - scrollDistance) 
             { 
                 transform.Translate(transform.forward * scrollSpeed * Time.deltaTime); 
             }
     }

}

Comment
Add comment · Show 2 · 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 Darker · Nov 01, 2014 at 10:01 PM 0
Share

What is the transform variable you're using?

avatar image Dexyne · Nov 10, 2014 at 01:56 PM 0
Share

@Darker It's the position of the object associated with the script (here it'll be the camera).

avatar image
0

Answer by JDB-Artist · Nov 17, 2012 at 08:54 PM

Many thanks for the solution! Could you give me an advise how to include a smoothing method in the camera scrolling? I think that the scrolling effect is a little abrupt/sudden and if possible I would like to smooth/fade in & out the camera movement.

Comment
Add comment · Show 1 · 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 eXtremeTY · Jan 16, 2013 at 08:36 PM 0
Share

I think you should look up into SmoothDamp if you want smoothness.. http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.SmoothDamp.html

avatar image
0

Answer by samsmithnz · Dec 27, 2012 at 10:17 PM

How do I limit mouse movements so that the user can't scroll too far away from my scene? With the keyboard I was able to use the following code, but I can't seem to find the equivalent with the mouse.

private float leftSide = -100f; private float rightSide = -80f; private float topSide = -80f; private float bottomSide = -100f; // Update is called once per frame void Update () { ///////////////////// //keyboard scrolling float translationX = Input.GetAxis("Horizontal"); float translationZ = Input.GetAxis("Vertical"); float toX = translationX + translationZ; if (transform.position.x + toX < leftSide) { toX = 0; } if (transform.position.x + toX > rightSide) { toX = 0; } float toZ = translationZ - translationX; if (transform.position.z + toZ > topSide) { toZ = 0; } if (transform.position.z + toZ < bottomSide) { toZ = 0; } transform.Translate(toX, 0, toZ); //////////////////// //mouse scrolling float mousePosX = Input.mousePosition.x; float mousePosY = Input.mousePosition.y; int scrollDistance = 1; float scrollSpeed = 70; //Horizontal camera movement //horizontal, left if (mousePosX < scrollDistance) { transform.Translate(-1, 0, 1); } //horizontal, right if (mousePosX >= Screen.width - scrollDistance) { transform.Translate(1, 0, -1); } //Vertical camera movement //scrolling down if (mousePosY < scrollDistance) { transform.Translate(-1, 0, -1); } //scrolling up if (mousePosY >= Screen.height - scrollDistance) { transform.Translate(1, 0, 1); } }

Comment
Add comment · Show 1 · 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 MattyWS · Jan 24, 2013 at 01:15 AM 0
Share

Read my comment right above yours

  • 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.

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

RTS Camera Zoom w/ Rotation 0 Answers

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

Camera Zoom problem 1 Answer

Fixing Issues with custom RTS Camera Controll Script 1 Answer

Changing camera position to shoot through scope on gun 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