- Home /
Movement relative to world axis&local rotation
I've just started out with Unity, so apologies if this is a stupid question, but I've searched high and low for an answer.
I have a camera that is pointing downwards at a 45 degree angle(45,0,0), and it will always remain at this angle. You can rotate the camera around a fixed point(A fake gameObject following the focal point of the camera). The bit I am struggling with is the panning of the camera. I want it to pan across a fix plane, so it should not move up or down, only left, right, forward or backwards. Because of the rotation the left/right movement is just a world.self translate, but when I try to translate the forward/backward movement it moves relative to the camera and starts to move along both the Y and Z axis(Essentially zooming in).
I tried setting the translate to self.world, which works ok at the start, but when you rotate the camera it continues to move along worlds Z axis which is wrong.
So I want to pan across the Z world axis, but in a relative direction to the cameras rotation.
I hope I explained that all ok :) Here's the parts of my movement code that are relevant.
transform.Translate(horzMoveAmt,0.0f,0.0f,Space.Self);
//Does not work when rotated
transform.Translate(0.0f,0.0f,vertMoveAmt,World.Self);
Answer by amphoterik · Jun 24, 2013 at 12:57 PM
You have two things that you can do. The first thing is to make the camera reset its x-axis rotation (0, 0, 0), then rotate it, and then rotate the x-axis back (45, 0, 0). Granted, this is a bit sloppy, but it will do what you want.
The second option is to actually nest the camera into the object that it is always looking at (or an empty object at the same location). then all you will need to do is rotate that object and the camera will rotate the way you want.
I tried to do this, with this code..
Quaternion tempRotate = transform.rotation;
transform.rotation = Quaternion.Euler(0, tempRotate.y, tempRotate.z);
transform.Translate(Vector3.forward * vert$$anonymous$$oveAmt);
transform.rotation = tempRotate;
But no joy, it is still moving relative to the world and not to the forward face of the camera. I will have to give the second method a bash, but I'm unsure why this first method is not working, it was my fallback sloppy code solution as well.
Say the camera is 5 units away (along z) from the object in question and you want it to rotate 20 degrees. A sloppy way would be like:
transform.Rotate(45, 0, 0); //reset it
transform.Translate(0, 0, 5); //move it to center
transform.Rotate(0, 20, 0); // rotate it
transform.Translate(0, 0, -5); //move it back
transform.Rotate(-45, 0, 0); look again
Like I said, very sloppy.
I got it work with this is in the end..
Vector3 tempRotate = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(0, tempRotate.y, tempRotate.z);
transform.Translate(0.0f, 0.0f,vert$$anonymous$$oveAmt, Space.Self);
transform.rotation = Quaternion.Euler(tempRotate.x, tempRotate.y, tempRotate.z);
This makes the programmer in me cry, but it will have to do for now. Putting the camera inside a game object has brought up a whole host of other problems(The game object rotating slower than the camera), so I will stick with this until I'm more comfortable with Unity. Thanks for the help!
That's why I suggested an empty game object so that you can control the speed of rotation.
I already marked as answered, I can't upvote yet unfortunately.
Answer by s_guy · Jun 25, 2013 at 12:10 AM
Subtract the location of the focal point from the location of the camera. Moving +/- along this vector will move the camera closer or farther. Save this off (or don't reevaluate) if you want the camera to stay on the same plane.
The cross product between Vector3.up and the vector determined above will yield a vector orthogonal to both. Moving the camera +/- along this vector will pan it.
Answer by daverabbich · Jun 24, 2013 at 12:31 PM
I think you should put the Camera in a gameobject and move that instead of trying to move the camera
Your answer
![](https://koobas.hobune.stream/wayback/20220613104738im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate object in 90 degrees relative to camera. 1 Answer
Copy world rotation cordinate 1 Answer
Instantiate gun & arms rig relative to rotation & position of first person controller camera 0 Answers
Fake 2D world rotation by moving elements of the world 1 Answer
Apply a Rotation in World Space based on Quaternions 2 Answers