Star☆Unityメモ

Unityのちょっとしたことをメモって後で見返せれたらいいなサイト

一点を中心に回転(TPS視点)

用意するもの

  1. Player(中心点にしたいObject)
  2. CenterRotation(回転を補助するObject)
  3. MainCamera(カメラ)

やること

  1. PlayerとCenterRotationのポジションを同期(この時子要素に入れない)
  2. RotationObjの子要素にCameraをセット.

 実践

(1)PlayerとCenterRotationのポジションを同期するするスクリプトを書いてあげる

   public GameObject player;
    private Vector3 offset;
    // Use this for initialization
    void Start () {
        offset = transform.position - player.transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        transform.position = player.transform.position + offset;
    }

(2)CenterRotationを回転させるスクリプト

void Update () {
    if (Input.GetKey(KeyCode.Q)) {
        this.transform.Rotate(new Vector3(0, 1, 0), 90 * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.E)) {
        this.transform.Rotate(new Vector3(0, 1, 0), -90 * Time.deltaTime);
    }
}

(3)RotationObjに(1)と(2)のスクリプトをアタッチ! コレで終わり!

ツリー

f:id:zvn_X:20170101140314p:plain