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 IamAnewUser · Jan 16, 2018 at 02:15 PM · camera-movementcamera rotatecamera movementsimulation

How to rotate and move the camera with correct orientation,How to move camera after it gets rotated

So I got stuck ... I'm making a simulation game and I want to be able to both move and rotate the camera. My code works when I'm using only one of the above mentioned BUT when used together they have not the desired result. eg first rotate the camera 90 degrees and after that try to move the camera forward then it will move to the left.

Here's my code :

     public Camera cam;
     public Transform camTransform;
     public GameObject centerPole;
     float secsHoldingRight = 0f;
     Vector3 lastMousePosition;
     public float rotationSensitivity = 1f;
     public float movingSensitivity = 10f;
 
     void Start() {
         camTransform = cam.gameObject.transform;
         centerPole = GameObject.Find ("centerOfScreen");
     }
 
     void Update () {
 
         MoveCamera();
 
         //Rotate camera
         if (Input.GetMouseButton (1)){
         secsHoldingRight += Time.deltaTime;
 
             if (secsHoldingRight > 0.2f) {
                 if (Input.mousePosition.x > lastMousePosition.x) {
                     RotateCamera (rotationSensitivity * Time.deltaTime * (Input.mousePosition.x - lastMousePosition.x));
                 } 
                 else if (Input.mousePosition.x < lastMousePosition.x) {
                     RotateCamera (rotationSensitivity * Time.deltaTime * (Input.mousePosition.x - lastMousePosition.x));
                 }
             }
         }
         else{
             secsHoldingRight = 0f;
         }
         lastMousePosition = Input.mousePosition;
     }
 
     void RotateCamera(float rotationAmount){
         cam.transform.RotateAround (Vector3.zero, Vector3.up, rotationAmount * 10f);
     }
 
     void MoveCamera()  {
         if (Input.mousePosition.y >= Screen.height * 0.95) {
             cam.transform.position += Vector3.forward * (Time.deltaTime * movingSensitivity);
         }
         else if (Input.mousePosition.y <= Screen.height * 0.05) {
             cam.transform.position += Vector3.back * (Time.deltaTime * movingSensitivity);
         }
 
         if (Input.mousePosition.x >= Screen.width * 0.95) {
             cam.transform.position += Vector3.right * (Time.deltaTime * movingSensitivity);
         }
         else if (Input.mousePosition.x <= Screen.width * 0.05) {
            cam.transform.position += Vector3.left * (Time.deltaTime * movingSensitivity);
         }
     }
 }
 

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
0

Answer by Dray · Jan 16, 2018 at 03:46 PM

You are moving your camera along the global xyz axis, if you move it along your cameras relative axises it will work.


For example; Vector3.left is always (-1, 0, 0), no matter what your cam rotation looks like and where you call it.

cam.transform.left will return a vector pointing to the left seen from your cameras relative space.


Try this:

 void MoveCamera()  {
      if (Input.mousePosition.y >= Screen.height * 0.95) {
          cam.transform.position += cam.transform.forward * (Time.deltaTime * movingSensitivity);
      }
      else if (Input.mousePosition.y <= Screen.height * 0.05) {
          cam.transform.position += cam.transform.back * (Time.deltaTime * movingSensitivity);
      }
 
      if (Input.mousePosition.x >= Screen.width * 0.95) {
          cam.transform.position += cam.transform.right * (Time.deltaTime * movingSensitivity);
      }
      else if (Input.mousePosition.x <= Screen.width * 0.05) {
         cam.transform.position += cam.transform.left * (Time.deltaTime * movingSensitivity);
      }
  }

This will make your movement be relative to your cameras transform

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

78 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 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 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 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

Mouse Look Help 2 Answers

My camera is not linking with my movement 0 Answers

Automatic Camera Rotation in the 3D Game Kit 0 Answers

Automatic 3rd person camera 1 Answer

URGENT Cinemachine virtual camera while falling 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