• 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

@WalidKhairy Change it's position directly instead 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Movement relative to Camera and XZ Plane 1 Answer

camera slides and bounces while moving C# 1 Answer

Global movement 0 Answers

Orient GameObject rotation to rotation of Main Camera 0 Answers

Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges