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 UANP_Joe · Sep 28, 2015 at 06:42 AM · c#camerarotationmovement

RTS Camera Rotation and Movement

Hello. I am new to scripting as well as c# and so far I have been going along with the Unity API and figuring things out by googling questions, but I can't seem to find an answer for this. I am making a RTS style camera, and I want to be able to rotate the camera's view with right click + drag, which I have managed to do with line 170 -> 175, but I can't get the direction the camera is heading when W is pressed to rotate along with it, and when I right click it seems the camera reverts to 0 on the X and Z axis rotation (the camera starts out looking at the ground). The script is attached to the camera itself and the camera is not attached to anything else. Any information you can help me with would be excellent. Thanks.

 using UnityEngine;
 using System.Collections;
 
 public class RTSCam01 : MonoBehaviour 
 {    
 
     //Map border constraints so the camera doesn't go off the map area//
     public float mapBorderForward = 37f;
     public float mapBorderBack = 50f;
     public float mapBorderLeft = 38f;
     public float mapBorderRight = 38f;
 
     //Screen constraints for edge scrolling//
     private int theScreenWidth = Screen.width;
     private int theScreenHeight = Screen.height;
 
     //Camera zooming constraints//
     public float cameraSpeed = 10f;
     public float cameraZoomSpeed = 30f;
     public float cameraZoomIn = 4f;
     public float cameraZoomOut = 10f;
 
     //Camera rotation constraints//
     private float mouseX;
     private float mouseY;
     public float cameraRotateSpeed = 1f;
     public float cameraVertRotateMin = 0f;
     public float cameraVertRotateMax = 40f;
 
     void Start()
     {
     }
 
     // Update is called once per frame
     void Update () 
     {
 
         //Mouse edge scroll forward//
         if (Input.mousePosition.y >= theScreenHeight * 0.98f)
         {
             transform.Translate(Vector3.forward * Time.deltaTime * cameraSpeed, Space.World);
             if (transform.position.z > mapBorderForward)
             {
                 Vector3 clampForward = transform.position;
                 clampForward.z = mapBorderForward;
                 transform.position = clampForward;
             }
             //Debug.Log (transform.position);
         }
 
         //Mouse edge scroll back//
         if (Input.mousePosition.y <= theScreenHeight * 0.02f)
         {
             transform.Translate(Vector3.back * Time.deltaTime * cameraSpeed, Space.World);
             if (transform.position.z < -mapBorderBack)
             {
                 Vector3 clampBack = transform.position;
                 clampBack.z = -mapBorderBack;
                 transform.position = clampBack;
             }
             //Debug.Log(transform.position);
         }
 
         //Mouse edge scroll left//
         if (Input.mousePosition.x <= theScreenWidth * 0.02f)
         {
             transform.Translate(Vector3.left * Time.deltaTime * cameraSpeed, Space.World);
             if (transform.position.x < -mapBorderLeft)
             {
                 Vector3 clampLeft = transform.position;
                 clampLeft.x = -mapBorderLeft;
                 transform.position = clampLeft;
             }
             //Debug.Log(transform.position);
         }
 
         //Mouse edge scroll right//
         if (Input.mousePosition.x >= theScreenWidth * 0.98f)
         { 
             transform.Translate(Vector3.right * Time.deltaTime * cameraSpeed, Space.World);
             if (transform.position.x > mapBorderRight)
             {
                 Vector3 clampRight = transform.position;
                 clampRight.x = mapBorderRight;
                 transform.position = clampRight;
             }
             //Debug.Log(transform.position);
         } 
 
         //W forward key//
         if (Input.GetKey(KeyCode.W)) 
         { 
             transform.Translate(Vector3.forward * Time.deltaTime * cameraSpeed, Space.World); 
             if (transform.position.z > mapBorderForward)
             {
                 Vector3 clampForwardKey = transform.position;
                 clampForwardKey.z = mapBorderForward;
                 transform.position = clampForwardKey;
             }
             //Debug.Log (transform.position);
         } 
 
         //S back key//
         if (Input.GetKey(KeyCode.S)) 
         { 
             transform.Translate(Vector3.back * Time.deltaTime * cameraSpeed, Space.World); 
             if (transform.position.z < -mapBorderBack)
             {
                 Vector3 clampBackKey = transform.position;
                 clampBackKey.z = -mapBorderBack;
                 transform.position = clampBackKey;
             }
             //Debug.Log(transform.position);
         }
 
         //A left key//
         if (Input.GetKey(KeyCode.A)) 
         { 
             transform.Translate(Vector3.left * Time.deltaTime * cameraSpeed, Space.World); 
             if (transform.position.x < -mapBorderLeft)
             {
                 Vector3 clampLeftKey = transform.position;
                 clampLeftKey.x = -mapBorderLeft;
                 transform.position = clampLeftKey;
             }
             //Debug.Log(transform.position);
         }
 
         //D right key//
         if (Input.GetKey(KeyCode.D))
         { 
             transform.Translate(Vector3.right * Time.deltaTime * cameraSpeed, Space.World); 
             if (transform.position.x > mapBorderRight)
             {
                 Vector3 clampRightKey = transform.position;
                 clampRightKey.x = mapBorderRight;
                 transform.position = clampRightKey;
             }
             //Debug.Log(transform.position);
         }
 
         //Mouse wheel scroll in//
         if(Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             Vector3 camDown = new Vector3(0, -1, 0);
             transform.Translate(camDown * cameraZoomSpeed * Time.deltaTime, Space.World);
             if (transform.position.y < cameraZoomIn)
             {
                 Vector3 camZoomInMax = transform.position;
                 camZoomInMax.y = cameraZoomIn;
                 transform.position = camZoomInMax;
             }
             Debug.Log(transform.position);
         }
 
         //Mouse wheel scroll out//
         if(Input.GetAxis("Mouse ScrollWheel") < 0)
         {
             Vector3 camUp = new Vector3(0, 1, 0);
             transform.Translate(camUp * cameraZoomSpeed * Time.deltaTime, Space.World);
             if (transform.position.y > cameraZoomOut)
             {
                 Vector3 camZoomOutMax = transform.position;
                 camZoomOutMax.y = cameraZoomOut;
                 transform.position = camZoomOutMax;
             }
             Debug.Log(transform.position);
         }
 
         //Right click and drag mouse rotate//
         if(Input.GetKey(KeyCode.Mouse1))
         {
             mouseX += Input.GetAxis("Mouse X");
             mouseY -= Input.GetAxis("Mouse Y");
             transform.eulerAngles = new Vector3(mouseY, mouseX, 0);
         }
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Flip over an object (smooth transition) 3 Answers

Orient GameObject rotation to rotation of Main Camera 0 Answers

RTS Camera movement wrong after rotation 1 Answer

how are Mathf.SmoothDamp and Mathf.SmoothStep different 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