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 WalidKhairy · May 24, 2015 at 02:59 PM · camerarotationmovementtranslateworldspace

[Solved] Translating Camera on only X, Z axis on World Space, no matter what is the rotation

I'm using a panning script for a top down camera, with a default localEulerAngles of (90, 0, 0) & it works fine in this default case

 void Update(){
     if(Input.GetMouseButtonDown(0))
     {
         mouseOrigin = Input.mousePosition;
         isPanning = true;
     }
     
     if (!Input.GetMouseButton(0)) 
              isPanning=false;
     
     if (isPanning)
     {
              Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
              Vector3 move = new Vector3(pos.x * panSpeed * -1, 0, pos.y * panSpeed * -1);
              transform.Translate(move, Space.World);
     }
 }
 

so dragging form up to down screen, makes the camera goes forward from left to right the screen, makes it translate to the left and so on for Down & right directions

the problem is once the camera being rotated in the y axis, the panning actions change, so the dragging up makes the camera goes right instead of forward, and dragging to the left makes the camera goes forward .. that's because i'm using Space.World

I tried using Space.Self, but when the camera being rotated in the x axis, Forward & Backward work as Up & Down.

I want to translate camera with touch, in X & Z axis, without caring about the camera rotation

Any one can help ? alt text

panning.png (79.0 kB)
Comment
Add comment · Show 3
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 Side · May 24, 2015 at 03:51 PM 1
Share

@Walid$$anonymous$$hairy Change it's position directly ins$$anonymous$$d of using Translate().

 transform.position += move;

Did it work?

avatar image WalidKhairy · May 24, 2015 at 06:06 PM 0
Share

@Side Ok, That solves the movement whatever the rotation around x is. i have to change Vector move to:

 Vector3 move = new Vector3(pos.y * panSpeed, 0, pos.x * panSpeed * -1);
 transform.position += move;

then it works BUT, when the camera is being rotated around Y axis, moving Forward & backward swapped with right & left !! Now what?

avatar image WalidKhairy · May 25, 2015 at 11:44 AM 0
Share

@Side ... ok, i figured it out I had to handle the case of rotation around Y axis, manually

     float ang = Camera.main.transform.localEulerAngles.y;
     // move vector ( forward/backward , up/down , right/left );
     if(ang >= 225 && ang < 315)
         move = new Vector3(pos.y * panSpeed, 0, pos.x * panSpeed * -1);
     else if(ang >= 45 && ang < 135)
         move = new Vector3(pos.y * panSpeed * -1, 0, pos.x * panSpeed);
     else if(ang >= 135 && ang < 315)
         move = new Vector3(pos.x * panSpeed, 0, pos.y * panSpeed);
     else if((ang >= 315 && ang < 360) || (ang >=0 && ang < 45))
         move = new Vector3(pos.x * panSpeed * -1, 0, pos.y * panSpeed * -1);
     
     transform.position += move;
 

Now it works fine

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

2 People are following this question.

avatar image avatar image

Related Questions

RTS Camera Rotation and Movement 0 Answers

camera slides and bounces while moving C# 1 Answer

World and Local Axis out of Alignment 1 Answer

Global movement 0 Answers

How to make my player look in the direction of its movement? 3 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