- Home /
How to pan-rotate camera transform parallel to its view plane?
Hello,
I tried tackling this problem for few days, but could not solve it.
So far I achieved a rotation along certain axis, but the desired effect is to rotate the camera along a circle-like path that is parallel to the camera's view "plane".
See the picture below:
[1] .
Goal: achieving a rotation shown with a blue arrow-path (parallel to the camera's view plane)
Thanks.
Answer by robertbu · May 14, 2014 at 03:19 AM
Your question is missing details for me to give any focused answer:
What defines the center and radius of the circle
Does the camera need to rotate with the circle or does its orientation remain fixed.
What caused the camera to move on the path
Approaches:
You can use RotateAround(). The axis of rotation would be the camera's transform.forward.
Or you could make the camera a child of an empty game object on the same plane with an offset and rotate the child object.
Or you can calculate the position based on a vector. You can use Quaternion.AngleAxis() to rotate the vector. Again transform.forward is the axis of rotation.
[What defines the center and radius of the circle] Circle's center I find by deducting a constant (int, that acts as a radius) from the last known X and Y.
From such I can find circle points on each of the degrees manually: nextX - new X coordinate that is the part of the circle path (without adjustment, which I don't know how to achieve yet)
cameraX - is the last known camera.transform.x
radius - is a constant that is manually defined
nextDegree - a value from 0.0f to 359.00f
**nextX = (cameraX - radius) + radius $$anonymous$$athf.Cos (nextDegree);* for Y is the same but with $$anonymous$$athf.Sin...
After having such I can make a Vector3(nextX, nextY, cameraZ), where cameraZ is the last known Z coordinate of the camera.
The whole point is camera transform was rotated in all 3 directions, making it less possible for me to use RotateAround, unless I am missing something.
I want to achieve a rotation parallel to the camera's view, but I cannot rotate the Z-axis with the camera, can I?
So as much as I understood I cannot use RotateAround function - it will not be "parallel" to the camera's view plane. (or once again I am missing on a point)
[Does the camera need to rotate with the circle or does its orientation remain fixed.] The blue arrow on phase 3 in the picture - is the path along which the camera would "slide/move/follow". So the circle (blue) - does not move, only the camera.
[What caused the camera to move on the path] I may not understand this question well, but I try answering it:
That's the effect I want to achieve. If you refer to the phases 1,2 of the picture - that's just a case of how the camera could be: 1) moved 2) rotated
So the main issue I have here is that "RotateAround" function will take the camera object and rotate it around the axis. But I need to rotate it in parallel to the camera's view plane (remember, I rotate the camera in all 3 directions, meaning that the camera's view plane is no longer parallel to any of the three axis)
You can use RotateAround(). The axis of rotation would be the camera's transform.forward.
Do correct me if I was wrong in the above comment to your answer, as I tried to do the rotation around the axis but I didn't have it working the way I wanted.
However, I will try doing it again just to ensure I had it right about, it was no way out for my case.
but I cannot rotate the Z-axis with the camera, can I? -- So as much as I understood I cannot use RotateAround function - it will not be "parallel" to the camera's view plane
$$anonymous$$y suggestion is to use 'transform.forward' of the camera, not Vector3.forward. 'transform.forward' is the forward vector of the camera in world space. So if you use this as an axis of rotation, then the rotation will be parallel to the camera plane. Here is a bit of sample source. Do:
Start a new scene
$$anonymous$$ove and rotate the camera to some arbitrary position and angle.
Put a game object in front of the camera offset some from the center of the frame. This will be your center object.
Attach the following script to the camera.
Initialize the 'center' variable by dragging the object to be used as center onto the 'center' variable in the Inspector.
Hit play and use the arrow keys.
Note it will appear as if the object is circling, but in reality the camera is circling...on its view plane.
#pragma strict
var center : Transform;
var speed = 90; // Degrees per second
private var v : Vector3;
function Start() {
v = transform.position - center.position;
}
function Update() {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
v = Quaternion.AngleAxis(speed * Time.deltaTime, transform.forward) * v;
transform.position = center.position + v;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
v = Quaternion.AngleAxis(-speed * Time.deltaTime, transform.forward) * v;
transform.position = center.position + v;
}
}
As you can see from this example, the object dos not have to be on the camera plane, but it will work if it is.
It does what I was looking after. Thank you, I will have to remember that there also is "transform.forward" and not only Vectro3. ^^
But what bothers me now is that I did the same in my project where I needed it, and it does not work (until I simply created a new scene and basically followed a step by step instruction outlined by you).
That part, however, I would tackle further on my own, since I got the mechanics known to me by now.
I appreciate your help, thank you very much.