Skip to content

Instantly share code, notes, and snippets.

@Islington036
Last active June 8, 2022 09:01
Show Gist options
  • Save Islington036/42a75a814ef3a29ad37305cb8ef6f9c2 to your computer and use it in GitHub Desktop.
Save Islington036/42a75a814ef3a29ad37305cb8ef6f9c2 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DirectionMark : MonoBehaviour
{
//=============================================================================
// 変数
//=============================================================================
// プレイヤーの情報
[SerializeField, Tooltip("プレイヤーオブジェクト")]
private Transform player = null;
// カメラ情報
[SerializeField, Tooltip("プレイヤーを映すカメラ")]
private Transform Camera = null;
// ターゲット
[SerializeField, Tooltip("追いかけるターゲット")]
private Transform Target = null;
//=============================================================================
// プロパティ
//=============================================================================
public Transform SetPlayer { get { return player; } set { player = value; } }
public Transform SetCamera { get { return Camera; } set { Camera = value; } }
public Transform SetTarget { get { return Target; } set { Target = value; } }
//=============================================================================
// アップデート
//=============================================================================
void Update()
{
TurnAroundDirectionTarget();
}
//=============================================================================
// 矢印を回転させる
//=============================================================================
private void TurnAroundDirectionTarget()
{
// プレイヤーからターゲットまでのベクトルを計算
Vector3 Direction = (Target.position - player.transform.position).normalized;
// 求めた方向への回転量を求める
Quaternion RotationalVolume = Quaternion.LookRotation(Direction, Vector3.up);
// カメラ情報を元に回転量の補正
Quaternion CorrectionVolume = Quaternion.FromToRotation(Camera.transform.forward, Vector3.forward);
// 向きを計算
Vector3 vec = (RotationalVolume * CorrectionVolume).eulerAngles;
// 向きを反映
transform.rotation = Quaternion.Euler(0, vec.y, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment