0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Unity]キャラクターへスムーズにカメラを向ける

Last updated at Posted at 2021-02-19

概要

Quaternion.LookRotationでプレイヤーが対象物を向く角度を得て、Quaternion.Lerpで回転運動の補間をしています。

コードの内容

using UnityEngine;

public class LookAtGameObject : MonoBehaviour
{
    public Transform[] position;
    int targetNumber = 0;
    float rotatePosition = 0f;
    float rotateSpeed = 0.1f;
    Vector3 direction;

    private void Update() {
        if (Input.GetMouseButtonDown(0)) {
            targetNumber++;
            if (targetNumber >= position.Length) targetNumber = 0;
            direction = position[targetNumber].position - transform.position;
            rotatePosition = 0f;
        }

        if (rotatePosition < 1f) {
            rotatePosition += rotateSpeed * Time.deltaTime;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction), rotatePosition);
        }
    }
}

設定

スクリーンショット (1805).png
MainCameraをPlayerに持たせます。(MainCameraのTransform各種を0にしてください)

LookAtGameObjectをPlayerに貼り付けてInspector内のpositionにカメラを向けたいオブジェクトをドラッグアンドドロップします。

実行

画面をクリックするたびにカメラが対象を次々映し続けます

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?