Last active
July 6, 2020 16:17
-
-
Save Tatsupuri/62913427ca837805421701723b02c121 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; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.UI; | |
public class DoorOpen: MonoBehaviour//扉に貼り付けるスクリプト | |
{ | |
public enum MYEVENT{ | |
Next //など他にも使いたいイベントを定義していく | |
} | |
public MYEVENT myEvent; | |
public enum SCENE{//遷移先を定義 | |
Title, Stage1, Stage2 | |
} | |
public SCENE nextScene; | |
[SerializeField] private GameObject player;//距離を測定する相手,すなわちプレイヤー | |
Transform playerTrans; | |
[SerializeField] float activeDistance = 5.0f;//扉がプレイヤーを認識する距離 | |
[SerializeField] GameObject icon//近づいた時に表示される絵を指定(例:四角ボタンの入力を促すなら四角ボタンのアイコンを表示) | |
void Start() | |
{ | |
icon.SetActive(false);//アイコンの初期化 | |
} | |
void Update() | |
{ | |
var distance = Vector3.Distance(transform.position, playerTrans.position);//プレイヤーと扉の距離 | |
Vector3 toOther = transform.position - playerTrans.position;//プレイヤーから見た扉の方向ベクトル | |
Vector3 forward = playerTrans.forward;//プレイヤーの前方を表すベクトル | |
if(distance < activeDistance && Vector3.Dot(forward, toOther) > 0){//距離と内積に関しての条件を満たせば以下を実行 | |
icon.SetActive(true);//近づくとアイコンを表示し,操作を促す | |
if(Input.GetButton("Submit")){ | |
if(myEvent.ToString() == "Next")//今はMYEVENTクラスにはNextしかないが,この手の操作は全てこれで扱える. | |
{ | |
SceneManager.LoadScene (nextScene.ToString());//nextSceneをString型にキャストして,その名前のシーンをロードする. | |
} | |
else | |
{ | |
icon.SetActive(false); | |
} | |
} | |
void OnDrawGizmosSelected()//シーンビューで見やすいようにactiveDistanceを図示する. | |
{ | |
Gizmos.color = Color.blue; | |
Gizmos.DrawWireSphere(transform.position, activeDistance); | |
} | |
} |
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 UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class Icon : MonoBehaviour { | |
RectTransform rectTransform = null; | |
[SerializeField] Transform target = null; | |
private Image image; | |
void Awake(){ | |
rectTransform = GetComponent<RectTransform> (); | |
} | |
void Update() | |
{ | |
rectTransform.position = RectTransformUtility.WorldToScreenPoint (Camera.main, target.position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment