Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 ps1ke · Jul 13, 2015 at 01:22 AM · camerarts

RTS game: Camera movement is wierd

I'm building an RTS game and I'm having a problem with the camera movement.

Whatever I move my mouse to the top sreen is not moving really moving the camera view in the front/left/right/back but instead is only changing the values in the axis and the movement is wierd.

So the green arrows is the movement I expect and the red arrows is the movement I get.

alt text

I want to say too that, In this game you can rotate the camera in the Y-axis in between 0 and 360 degrees. And in any rotation I want my camera to behave ALWAYS like the GREEN arrows.

  #pragma strict
   
   private var delta : Vector3 = Vector3.zero;
   private var lastPos : Vector3 = Vector3.zero;
   
   public var Boundary : int = 50; // distance from edge scrolling starts
   public var speed : int = 5;
   
   private var theScreenWidth : int;
   private var theScreenHeight : int;
   private var rotationSpeed : float;
   
   var minFov: float = 2f;
   var maxFov: float = 3f;
   var sensitivity: float = 10f;
   
   function Start() 
   {
       theScreenWidth = Screen.width;
       theScreenHeight = Screen.height;
   }
   
   function Update() 
   {
   
        var yCam : float = Camera.main.transform.eulerAngles.y;
   
       if (Input.mousePosition.x > theScreenWidth - Boundary)
       {
          transform.position.x += speed * Time.deltaTime; // move on +X axis
       }
   
       if (Input.mousePosition.x < 0 + Boundary)
       {
          transform.position.x -= speed * Time.deltaTime; // move on -X axis
       }
   
       if (Input.mousePosition.y > theScreenHeight - Boundary)
       {
          transform.position.z += speed * Time.deltaTime; // move on +Z axis
       }
   
       if (Input.mousePosition.y < 0 + Boundary)
       {
          transform.position.z -= speed * Time.deltaTime; // move on -Z axis
       }  
      
      //zoom cam
         var fov: float = Camera.main.orthographicSize;
         fov += -(Input.GetAxis("Mouse ScrollWheel")) * sensitivity;
         fov = Mathf.Clamp(fov, minFov, maxFov);
         Camera.main.orthographicSize = Mathf.Lerp (Camera.main.orthographicSize, fov, Time.deltaTime * sensitivity);
         
         //rotate cam
         if(Input.GetMouseButton(2)){
             if(Input.mousePosition.x > theScreenWidth/2){
                 lastPos = Input.mousePosition;
                 delta = Input.mousePosition - lastPos; 
                 transform.eulerAngles -= new Vector3(0,(theScreenWidth/2-(lastPos.x))*0.01,0);
             }
             
          if(Input.mousePosition.x < theScreenWidth/2){
                 lastPos = Input.mousePosition;
                 delta = Input.mousePosition - lastPos;
                 transform.eulerAngles += new Vector3(0,((lastPos.x)-theScreenWidth/2)*0.01,0);
             }
  
     }
     }
  
screenshot-24.png (12.6 kB)
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

2 Replies

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

Answer by troien · Jul 13, 2015 at 07:48 AM

When editing the position, you should make sure it properly handles the current y-axis rotation of your camera.

Changing these lines (all 4 if's, not just this one)

 if (Input.mousePosition.x > theScreenWidth - Boundary)
 {
     transform.position.x += speed * Time.deltaTime; // move on +X axis
 }

To something like this:

 if (Input.mousePosition.x > theScreenWidth - Boundary)
 {
     Quaternion rotation = Quaternion.Euler(0,transform.rotation.eulerAngles.y,0);
     Vector3 movement = new Vector3(speed * Time.deltaTime, 0, 0); // move on +X axis
     
     transform.position += rotation * movement;
 }


Should work. (If it doesn't, please reply in the comments :p)

(My example is in C# because I'm used to coding in C#, but I think you'll be able to convert this to Javascript yourself)

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 ps1ke · Jul 14, 2015 at 12:52 AM 0
Share

It's not working... multiplying vectors is not going to work because there are 0 values in them

EDIT: I'm an idiot

avatar image
0

Answer by graciasluigi · Aug 01, 2015 at 07:30 PM

This is a RTS camera I'have created some time ago. Maybe it can help someone. Post Github link

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RTS camera 1 Answer

Pan RTS camera controller without changing elevation/zoom 0 Answers

Camera Zoom problem 1 Answer

Fixing Issues with custom RTS Camera Controll Script 1 Answer

Making an RTS camera 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