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 00george · Jan 04, 2014 at 12:38 PM · rotationangleworldspacelocalspace

Moving forward in world space with rotation.

Hi, I have been trying to make a RTS like camera and so far I have been stuck with a problem. I have managed to get the camera to rotate, zoom in and out and pan left and right but when I try to pan forward I get a problem because it works fine if I dont get rotate the camera but if I do it does not take the rotation into account. I know it is something to do with world space and local space and have researched these topics but I am still stuck because if I move forward in local space it does include rotation but does not scrol along the Z axis instead goes forward into the ground because I have the camera on a angle but if I use world space it does not include rotation. My script so far is down below.Finally I have tried using transform.TransformDirection but wasn't sure how to properly used it. I also have a diagram to help illustrate the problem : alt text

 using UnityEngine;
 using System.Collections;
 
 public class cameraControls : MonoBehaviour {
 
     public float scrollSpeed, cameraSpeed;
     public float pixelEdge = 10f;
     private float mouseX,mouseY;
     private bool isZooming, isRotating, isTranslation;
     private bool controlsOn;
 
     public Vector3 movement = new Vector3(0,0,0);
 
     // Use this for initialization
     void Start () {
         controlsOn = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown(KeyCode.C)){
             controlsOn = !controlsOn;
         }
     
     }
 
     void HandleMouseRotation(){
         float easeFactor = 10f;
         if(Input.GetMouseButton(1) && Input.GetKey(KeyCode.LeftControl)){
             if(Input.mousePosition.x != mouseX){
                 float cameraRotationY = (Input.mousePosition.x - mouseX) * easeFactor * Time.deltaTime;
                 this.transform.Rotate(0,cameraRotationY,0,Space.World);
             }
         }
 
     }
 
     void Zoom(){
         //Zoom
         if(Input.GetAxis("Mouse ScrollWheel") > 0){
             //transform.Translate(Vector3.forward * Time.deltaTime * scrollSpeed);
             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView,Camera.main.fieldOfView - scrollSpeed,Time.deltaTime);
         }
         if(Input.GetAxis("Mouse ScrollWheel") < 0){
             //transform.Translate(Vector3.forward * Time.deltaTime * scrollSpeed);
             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView,Camera.main.fieldOfView + scrollSpeed,Time.deltaTime);
         }
     }
 
     void cameraTranslation(){
         //Translate    
         if(Input.mousePosition.x > Screen.width - pixelEdge){
             transform.Translate(Vector3.right * Time.deltaTime * cameraSpeed,Space.Self);
         }
         
         if(Input.mousePosition.x < 0 + pixelEdge){
             transform.Translate(Vector3.left * Time.deltaTime * cameraSpeed,Space.Self);
         }
         
         if(Input.mousePosition.y > Screen.height - pixelEdge){
             transform.Translate(Vector3.forward * Time.deltaTime * cameraSpeed,Space.World);
 
 
         }
         
         if(Input.mousePosition.y < 0 + pixelEdge){
             transform.Translate(-Vector3.forward * Time.deltaTime * cameraSpeed,Space.World);            
         }
 
     }
 
     void LateUpdate(){
         if(controlsOn == true){
             HandleMouseRotation();
             Zoom();
             cameraTranslation();
         }
         mouseX = Input.mousePosition.x;
         mouseY = Input.mousePosition.y;
 
     }
 }
 


diagram.png (365.2 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

1 Reply

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

Answer by BigRoy · Jan 04, 2014 at 09:50 PM

Try this:

 Vector direction = transform.forward;
 direction.y = 0;
 direction.Normalize();
 transform.Translate(direction * Time.deltaTime * cameraSpeed, Space.World);

Note that this will not work properly if the camera is aiming (perfectly) straight down or up as that would result in a zero vector.

You could also do it with Quaternion.LookRotation using the scene up (Y-axis) as forward direction and the camera forward as it's up-axis. This way it's never at an angle and only rotating around the Y-axis.

 Quaternion quat = Quaternion.LookRotation(Vector3.up, transform.forward);

Though that quaternion rotation makes the Z-axis point scene up and then tries to aim the Y-axis to the camera's forward (so without an angle). Not that useful you would think, though this means that if we apply this rotation to a Vector3.up it will result in the camera's forward without the angle.

So in theory (at least in my head) using the quaternion from before we can do:

 Vector3 direction = Vector3.up * quat;
 transform.Translate(direction * Time.deltaTime * cameraSpeed, Space.World);

Out of the top of my head so sorry for any errors. Hope this helps. :)

Comment
Add comment · Show 4 · 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 00george · Jan 04, 2014 at 10:25 PM 0
Share

Thank you so much.

avatar image BigRoy · Jan 05, 2014 at 12:34 AM 0
Share

If this worked make sure to accept the answer, that way others who got here by googling will know whether this is an actual solution. :)

avatar image D3m0n · Apr 08, 2015 at 09:07 PM 0
Share

Thanks man!!

avatar image jacobjones · May 06, 2016 at 04:39 AM 0
Share

Thank you so much!

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

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

Related Questions

Vector2.Angle questions and confusion. 1 Answer

Clamping rotation doesn't work when local axis is different from global 0 Answers

Transform.rotation is setting local rotatoin 0 Answers

Idk what's wrong with my code.. 1 Answer

Derive world space rotation given 2 points in space? 0 Answers


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