LoginSignup
0
2

More than 5 years have passed since last update.

unity > Main Cameraをぐるぐる回す

Last updated at Posted at 2015-08-08
動作確認
Unity 5.1.1-f on MacOS X 10.8.5

原点(0, 0, 0)にある物体に対して、カメラの方位を色々変えながら物体を映したい。

準備

  1. CubeをCreate (Scale: Z=2) : 回転がわかるようにZを2倍に
  2. Main CameraにCameraRotation.cs(以下)を関連付ける
  3. Main CameraのMy TransformにCubeを関連付ける

code

CameraRotation.cs
using UnityEngine;
using System.Collections;

public class CameraRotation : MonoBehaviour {

    public Transform myTransform;

    private float s_phi_ang = 0.0f;
    private const float s_dist = 10.0f;
    private const float kYPos = 6.0f;

    [Range(-2.0f, 2.0f)]
    public float rotationSpeed = 0.5f; // degree

    float ToRadian(float deg)
    {
        return deg * Mathf.PI / 180.0f;
    }

    void SetPosition()
    {
        float xpos = s_dist * Mathf.Sin (ToRadian (s_phi_ang));
        float zpos = s_dist * Mathf.Cos (ToRadian (s_phi_ang));
        Vector3 vec = new Vector3 (xpos, kYPos, zpos);
        transform.position = vec;

        transform.LookAt (myTransform);
    }

    void AddPhiAngle()
    {
        s_phi_ang += rotationSpeed;
    }

    void Start () {
        SetPosition ();
    }

    void Update () {
        AddPhiAngle ();
        SetPosition ();
    }
}

rotationSpeed[-2.0,2.0]にて回転の速度と方向を変更できる。

画像

Untitled_-_150808_cameraRotation_-_PC__Mac___Linux_Standalone__Personal_.jpg

Untitled_-_150808_cameraRotation_-_PC__Mac___Linux_Standalone__Personal_2.jpg

Untitled_-_150808_cameraRotation_-_PC__Mac___Linux_Standalone__Personal_3.jpg

https://gist.github.com/keijiro/3330732
を参考にアニメーションGIFを作ってみた。1MB以内で以下のようになった。

out.gif

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2