- Home /
[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 ?
@Walid$$anonymous$$hairy Change it's position directly ins$$anonymous$$d of using Translate().
transform.position += move;
Did it work?
@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?
@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
Your answer
Follow this Question
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