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
1
Question by helmi77 · Feb 01, 2012 at 01:48 PM · c#cameramovement

Camera Script - Bird's Eye View

I'm trying to write a camera script which allows the player to view the scene from a bird's eye view as seen in most Real Time Strategy games. The zoom function that moves the camera along the Y axis was not much of a problem.

I'm having troubles with the rotation and movement. For the latter I used the mouse movement so that the camera moves along the positive X axis as soon as the mouse hits the right edge of the game window. Left, Forward and Backwards are done similarly.

 // Camera movement
 private void HandleMovement()
 {
     Vector3 mousePosition = Input.mousePosition;
     Vector3 movementDirection = Vector3.zero;

     // Left
     if (mousePosition.x <= leftBoundary)
     {
         movementDirection = Vector3.left * MovementSpeed;
     }
     // Right
     else if (mousePosition.x >= rightBoundary)
     {
         movementDirection = Vector3.right * MovementSpeed;
     }
     // Backwards
     if (mousePosition.y <= topBoundary)
     {
         movementDirection = Vector3.back * MovementSpeed;
     }
     // Forward
     else if (mousePosition.y >= bottomBoundary)
     {
         movementDirection = Vector3.forward * MovementSpeed;
     }

     transform.Translate(movementDirection * Time.deltaTime, Space.World);
 }

Due to the Space.World Parameter in Translate() the camera will always consider the global +Z axis as it's forward direction. This means the camera won't move forward into the direction it is facing when rotated around the Y axis but rather forward along the world's Z axis. Switching to Space.Self doesn't seem to be a solution since the camera should always move orthogonal to the Y axis.

I was hoping that somebody could help me finding a solution for the camera's rotation.

edit: Rotation Method & fixed title

 private void HandleRotation()
 {
     if (Input.GetMouseButton(2))
     {
         currentMousePosition = Input.mousePosition.x;

         float rotation = 0.0f;

         if (currentMousePosition < previousMousePosition)
         {
             rotation = -RotationSpeed * Time.deltaTime;
         }
         else if (currentMousePosition > previousMousePosition)
         {
             rotation = RotationSpeed * Time.deltaTime;
         }

         transform.RotateAround(Vector3.up, rotation);

         previousMousePosition = currentMousePosition;
     }
 }


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 senad · Feb 01, 2012 at 02:11 PM 0
Share

In your code, I do not see you rotate the camera. Is there something missing?

1 Reply

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

Answer by helmi77 · Feb 16, 2012 at 03:55 PM

I finally found a solution to this problem without altering the rotation method at all. The only thing I edited is the last line of the movement method from

 transform.Translate(movementDirection * Time.deltaTime, Space.World);

to

 transform.localPosition += transform.localRotation * movementDirection;
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

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

Edit this code? 0 Answers

Move the camera to the left when the mousecursor is left 1 Answer

No Glitch Please ... 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