Last active
December 15, 2020 22:57
-
-
Save tubakihimeLoveHate/523451c2e79b62dbd9b668e38456d27d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PinSystemOverlay : MonoBehaviour | |
{ | |
private Camera targetCamera; // 映っているか判定するカメラへの参照 | |
private Transform canvas; | |
[SerializeField] | |
private GameObject pinPrefab; | |
private GameObject pinUI; | |
void Start() | |
{ | |
targetCamera = Camera.main; | |
canvas = GameObject.Find("Canvas").transform; | |
pinUI = Instantiate(pinPrefab, canvas); | |
pinUI.transform.SetParent(canvas); | |
Vector2 screenPosition = GetScreenPosition(transform.position); | |
Vector2 localPosition = GetCanvasLocalPosition(screenPosition); | |
pinUI.transform.localPosition = localPosition; | |
} | |
void LateUpdate() | |
{ | |
Vector3 offset = targetCamera.transform.position - transform.position; | |
Vector3 normal = offset.normalized; | |
RaycastHit hit; | |
if (Physics.Raycast(transform.position, normal, out hit, 20)) | |
{ | |
Vector3 cameraNormal = Vector3.Normalize(transform.position - targetCamera.transform.position); | |
float dot = Vector3.Dot(cameraNormal, targetCamera.transform.forward); | |
if (hit.collider.gameObject.name == targetCamera.gameObject.name && dot > 0.6f) | |
{ | |
pinUI.SetActive(true); | |
Vector2 screenPosition = GetScreenPosition(transform.position); | |
Vector2 localPosition = GetCanvasLocalPosition(screenPosition); | |
pinUI.transform.localPosition = localPosition; | |
} | |
else | |
{ | |
pinUI.SetActive(false); | |
} | |
} | |
} | |
/* もし判定がおかしければこちらも合わせてつけてみてください。 | |
private void OnBecameVisible() | |
{ | |
pinUI.SetActive(true); | |
} | |
private void OnBecameInvisible() | |
{ | |
pinUI.SetActive(false); | |
} | |
*/ | |
private Vector2 GetCanvasLocalPosition(Vector2 screenPosition) | |
{ | |
return canvas.transform.InverseTransformPoint(screenPosition); | |
} | |
private Vector2 GetScreenPosition(Vector3 worldPosition) | |
{ | |
return RectTransformUtility.WorldToScreenPoint(targetCamera, worldPosition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment