0
0

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 1 year has passed since last update.

[Unity] プレイヤーを基準に、ドアを常に開き戸にする

Last updated at Posted at 2023-06-21

はじめに

今回はプレイヤーがドアを開ける際、どの方向からでも常にドアが開き戸となるようにする方法を説明します。
開き戸にするために、ドア本体を回転させて実装する方法になります。
Videotogif (2).gif

実装

プレイヤーがドアを開ける際の向きを求める方法は主に以下の2つが考えられます。

1.プレイヤーの位置とドアの位置を比較する。
2.ドアと特定のオブジェクト間に線を引き、その線を基準にプレイヤーの角度を求める。
今回は、2の方法を採用しました。

2つのベクトル間の角度を求める

これを実装する関数として、SignedAngle() が利用できます。

SignedAngle
書式: public static float SignedAngle (Vector3 from, Vector3 to, Vector3 axis);
fromには基準となるベクトル、toにはもう一方のベクトルを指定します。axisには、向きの基準を決める軸を指定します。(後に詳細記述してます)
戻り値: axisベクトルの方向に対して時計回りの角度は正の値、反時計回りの角度は負の値となります。角度は-180度から180度の範囲で表されます。

それぞれのベクトルを始点から終点への「方向」を示すものとして計算します。具体的には、「fromObjからdoorへの方向」を示すベクトルaと、「playerからdoorへの方向」を示すベクトルbを計算します。
fromに設定するオブジェクトは、ドア横の左側に配置します。toには、doorを設定します。axisには、回転の軸にしたいy軸を設定します。ここではVector3.upを使います。。

Vector3 a = fromObj.position - door.position;
Vector3 b = player.position - door.position;
float angle = Vector3.SignedAngle(a, b, Vector3.up);

図にすると次のようなイメージです。
Game Idea-35.jpg
Game Idea-34.jpg
これで、ドアを中心にfromオブジェクトとPlayerの角度を求められました。

fromオブジェクトの配置

縦と横にドアを設置した場合、fromオブジェクトを適切な位置に配置するための条件式を次のように書きました。

c#

if ((45 <= startDoor && startDoor < 135) || (225 <= startDoor && startDoor < 315))
    fromObj.localPosition = new Vector3(door.localPosition.x,door.localPosition.y,door.localPosition.z - 5);
else
    fromObj.localPosition = new Vector3(door.localPosition.x + 5, door.localPosition.y, door.localPosition.z);

初期値のドアの向きで回転度を変更する

if ((45 <= startDoor && startDoor < 135) || (225 <= startDoor && startDoor < 315))
{
    //doorの向きを変更
    if (angle < 0)
        door.rotation = Quaternion.Euler(0, 90f, 0);
    else
        door.rotation = Quaternion.Euler(0, 270f, 0);
}
else
{
    if (angle < 0)
        door.rotation = Quaternion.Euler(0, 0f, 0);
    else
        door.rotation = Quaternion.Euler(0, 180f, 0);

Quaternion.Euler()は特定のオブジェクトを指定した角度で回転させる関数です。ここではy軸を中心にドアを回転させています。

スクリプト


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class BackRoomsDoor : MonoBehaviour
{
	[SerializeField] private Transform door;
	[SerializeField] private Transform fromObj;
    [SerializeField] private Transform player;
    private float angle = 0f;
    private float startDoor;
    Vector3 a = Vector3.zero;
    Vector3 b = Vector3.zero;

    void Start()
    {
        //doorのy軸のlocal rotationを取得
        startDoor = door.localEulerAngles.y;
        if ((45 <= startDoor && startDoor < 135) || (225 <= startDoor && startDoor < 315))
            fromObj.localPosition = new Vector3(door.localPosition.x, door.localPosition.y, door.localPosition.z - 5);
        else
            fromObj.localPosition = new Vector3(door.localPosition.x + 5, door.localPosition.y, door.localPosition.z);
    }

    private void Update()
    {
        a = fromObj.position - door.position;
        b = player.position - door.position;
        //aとbの2つのベクトル間の角度を-180度から180度の範囲で返します。
        angle = Vector3.SignedAngle(a, b, Vector3.up);
        if ((45 <= startDoor && startDoor < 135) || (225 <= startDoor && startDoor < 315))
        {
    		//doorの向きを変更
            if (angle < 0)
                door.rotation = Quaternion.Euler(0, 90f, 0);
            else
                door.rotation = Quaternion.Euler(0, 270f, 0);
        }
        else
        {
            if (angle < 0)
                door.rotation = Quaternion.Euler(0, 0f, 0);
            else
                door.rotation = Quaternion.Euler(0, 180f, 0);
         }
    }
}

参考記事

GitHub

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?