旋转摄像机角度

时间:2015-03-11 19:49:26

标签: c# unity3d

我正在尝试实例化围绕对象形成圆圈的多个相机。现在创建形成圆形的相机正在工作,但我还想旋转相机偏转角度,以便它也可以在每一步从不同角度观察物体。这是我的试用版:

int numberOfObjects = 50;
    for (int i = 0; i < numberOfObjects; i++)      
    {
       float angle = i * Mathf.PI * 2 / numberOfObjects;
       Vector3 pos =  new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * 10;
       var cameraGameObject = new GameObject( "Main Camera" );
       var camera = cameraGameObject.AddComponent<Camera>();
        camera.transform.rotation = Quaternion.Euler(0, angle,0);
       camera.transform.position = pos;
    }

2 个答案:

答案 0 :(得分:1)

在创建和设置新相机的变换后使用此功能

 // lookTarget is gameobject reference to where the camera will look at.
 camera.transform.LookAt(lookTarget.transform);

以下是更多信息的文档链接http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

编辑:解决方案如果您没有目标,可以创建临时目标。

    // declaring temporary gameobject;
    GameObject tempTarget;
    // instantiate a primitive cube
    tempTarget = GameObject.CreatePrimitive(PrimitiveType.Cube);
    // allowing your script make pop your circle from 0,0,0 position
    // it's meanning that your circle center is at Vector3.zero too.
    tempTarget.transform.position = new Vector3.zero;

    for (int i = 0; i < numberOfObjects; i++)
    {
        float angle = i * Mathf.PI * 2 / numberOfObjects;
        Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * 10;
        var cameraGameObject = new GameObject("Main Camera");
        var camera = cameraGameObject.AddComponent<Camera>();
        camera.transform.rotation = Quaternion.Euler(0, angle, 0);
        camera.transform.position = pos;
        // add the look at function
        camera.transform.LookAt(tempTarget.transform);

    }

    Destroy(tempTarget);

答案 1 :(得分:0)

使用Transform.RotateAround

t.RotateAround(t.position, Vector3.up, alpha);

其中:

  • t是相机变换。
  • alpha是您想要旋转的角度 (以度为单位!)

使用Mathf.Rad2Deg将弧度(在您的示例中)转换为度数。

在您的情况下,它看起来像:

alpha = angle*Mathf.Rad2Deg + 90;

(未经测试)