Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 FusionSausage · Oct 05, 2013 at 08:20 PM · cameramovement3dcamera rotaterts

3D RTS camera

Hi! I've been struggeling with a problem now for at least 5 days. I need a camera that should ALWAYS stay on the same Y-axis but it should be able to move on the X and Z-axis. It should also be rotateable horizontally and a bit vertically (say 60 degrees maybe). It would essentialy be like an RTS camera but 3D and rotatable. I'd also like it to have some zoom. How would I make this? Logic and/or scripts are appreciated! :)

Thanks!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cherno · Oct 05, 2013 at 09:43 PM

I had to create something similar for an isometric game, here is what I did:

I created an empty gameobject as a "Camera Helper", then made the Camera a child of it, and set the camera's transform to my liking (position from parent, rotation etc.). Just make sure that the camera's viewport center is pointing directly at the helper object.

The camera helper is set at whatever position you wish, and if want your camera to move, you instead just move and roate the helper object (along the x and z axes, if you wish). This means you get to use things like transform.forward which is very helpful.

Note that for rotations around the y axis (sideways), you should rotate the helper, and for the x axis (up&down), the camera itself.

Here's some camera move an zoom code. The script is attached to the camera.

You can ignore everything about the orthographic mode if you camera is perspective, and vice versa. I just liked to have the option of changing it during runtime for specific camera shots.

 var CameraMover : GameObject;
     
     var orthZoomStep : float= 10.0f;
     var orthZoomMaxSize : int = 500;
     var orthZoomMinSize : int = 300;
      
     var orthographicView : boolean;//set to true if the Camera is set to orthographic.
     
     function Start()
     {
          if(GetComponent(Camera).orthographic == true)
          {
               orthographicView = true;
          }
     }
     
     function Update () 
     {
     
         if(Input.GetKey(KeyCode.UpArrow))
         {
             CameraMover.transform.Translate(Vector3.forward * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.DownArrow))
         {
             CameraMover.transform.Translate(-Vector3.forward * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.LeftArrow))
         {
             CameraMover.transform.Translate(-Vector3.right * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.RightArrow))
         {
             CameraMover.transform.Translate(Vector3.right * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow))
         {
             CameraMover.transform.Translate((Vector3.forward + (-Vector3.right)) * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
         {
             CameraMover.transform.Translate((Vector3.forward + Vector3.right) * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))
         {
             CameraMover.transform.Translate((-Vector3.forward + (-Vector3.right)) * ScrollSpeed);
         }
         
         if(Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))
         {
             CameraMover.transform.Translate((-Vector3.forward + Vector3.right) * ScrollSpeed);
         }
     
    
         zoomCamera();
     }
     
     function zoomCamera()
     {
       
         // zoom out
         if (Input.GetAxis("Mouse ScrollWheel") < 0)//Mouse Wheel Down
         {
             if(orthographicView)
             {
                 if (camera.orthographicSize <=orthZoomMaxSize)
                 {
                     camera.orthographicSize += orthZoomStep;
                 }
                     
             }
             else
             {
                 if (camera.fieldOfView<=150)
                 {
                     camera.fieldOfView +=5;
                 }
                        
             }
         }
         // zoom in
         if (Input.GetAxis("Mouse ScrollWheel") > 0)//Mouse Wheel Up
            {
             if(orthographicView)
             {
                 if (camera.orthographicSize >= orthZoomMinSize)
                 {
                     camera.orthographicSize -= orthZoomStep; 
                 }
                                
             }
             else
             {
                 if (camera.fieldOfView>2)
                 {
                     camera.fieldOfView -=5;
                 }  
             }
      }
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 meat5000 · Oct 05, 2013 at 08:22 PM

Don't know if this is a good method or not;

Add a rigidbody to the camera and mark it as Kinematic. In the rigidbody you can then set Constraints to freeze movement and rotation in the three global axes. Kinematic will mean that it will move only under script control.

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 Elowan · Oct 07, 2013 at 12:17 PM

There is One Thing missing for a really perfect 3D RTS Cam: Zoom and center the Cam on a Gameobject, when click let us say left mouse & ALT. If clicked, the Cam First center smooth to the gameobject and if clicked again, a smooth zoom (by distance, not by FOV) is performed.

Any ideas on how to implement this here?

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

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

18 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 avatar image avatar image

Related Questions

Limiting vertical camera rotation 0 Answers

Camera Movement and angles 2 Answers

Move relative to screen 0 Answers

UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers

RTS Mouse Click Movement 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