0

I have a board on the world X,Z axis. I have a camera that can rotate to look downward or upward and rotate left to right using the world Y axis.

Here's the camera rotation code:

float angle = (mouseX_Current - mouseX_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(0.0f, angle, 0.0f, Space.World);

angle = (mouseY_Current - mouseY_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);

Using mouse button down and a mouse position input(drag&Drop) I translate the camera in the world. It receive a 2D vector that contain the X and Y value of the mouse(drag&drop) operation. Here's the code of the method I use :

private void CameraOnPlaneTranslation(Vector2 myVector)
{

   float cameraYPos = Camera.main.transform.position.y;

   Camera.main.transform.Translate(new Vector3(myVector[0], 0.0f, myVector[1]));

   Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, cameraYPos, Camera.main.transform.position.z);
}

Problem : When my camera is not oriented directly toward the Z Axis(0.0f, Z, 0.0f) the translation vector I compute is incorrect. I need to change the Y value of the vector so the camera don't move on the world Y axis.

Thanks

4

2 回答 2

1

I am not familiar with Unity, but supposing it is similar to other 3D systems on these points, I will attempt an answer and hopefully not embarrass myself.

It looks like you /translate/ the camera position, then immediately /set/ the camera position, thus overwriting the work done by translate. If this is the case, you should try setting camera.main.transform.position once (in the line after you create the Camera). Then only apply myVector by the Translate call (as you have it). Thus you should eliminate the third line of the method Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, cameraYPos, Camera.main.transform.position.z);

After trying this, if it still does not do what you expect, see if it needs you to Update the view or Flush or ApplyTransform . . . anything that will make it understand you are done altering the transform and you are ready for it to process again with the updated transform.

于 2013-10-28T02:58:54.347 回答
0

I've solved this by adding another GameObject that is affected only by the camera right/left rotations(world up vector) and the camera translations.

In the Awake method

cameraOrientation = new GameObject("cameraOrientation");
cameraOrientation.transform.position = Camera.main.transform.position;
cameraOrientation.transform.localEulerAngles = new Vector3(0.0f, Camera.main.transform.localEulerAngles.y, 0.0f);

In the rotation method

float angle = (mouseX_Current - mouseX_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(0.0f, angle, 0.0f, Space.World);
cameraOrientation.transform.Rotate(0.0f, angle, 0.0f, Space.World);

angle = (mouseY_Current - mouseY_ActionStart) * camRotationSpeedMod;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);

And finally the translation method

cameraOrientation.transform.Translate(new Vector3(myVector[0], 0.0f, myVector[1]));
Camera.main.transform.position = cameraOrientation.transform.position;
于 2013-10-28T15:20:57.963 回答