gimm270_07_photogallery/Assets/Scripts/Proximity.cs

60 lines
1.5 KiB
C#
Raw Permalink Normal View History

2022-09-18 23:23:50 -06:00
using UnityEngine;
2022-09-18 22:04:12 -06:00
using UnityEngine.UI;
public class Proximity : MonoBehaviour
{
public string newTitle;
public string newDesc;
private Transform other;
private Text myTitle;
private Text myDesc;
private float dist;
2022-09-18 23:23:50 -06:00
public float minDistance = 4.0f;
public float maxDistance = 5.0f;
2022-09-18 22:04:12 -06:00
private GameObject player;
private GameObject message1;
private GameObject message2;
private bool check;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag("Player");
other = player.GetComponent<Transform>();
message1 = GameObject.FindWithTag("ArtTitle");
2022-09-18 23:23:50 -06:00
message2 = GameObject.FindWithTag("Description");
2022-09-18 22:04:12 -06:00
myTitle = message1.GetComponent<Text>();
myTitle.text = "";
2022-09-18 23:23:50 -06:00
myDesc = message2.GetComponent<Text>();
2022-09-18 22:04:12 -06:00
myDesc.text = "";
check = false;
}
// Update is called once per frame
void Update()
{
if (other)
{
dist = Vector3.Distance(transform.position, other.position);
print("Distance to player: " + dist);
2022-09-18 23:23:50 -06:00
if (dist < minDistance)
2022-09-18 22:04:12 -06:00
{
myTitle.text = newTitle;
myDesc.text = newDesc;
check = true;
}
2022-09-18 23:23:50 -06:00
if (dist > maxDistance && check)
2022-09-18 22:04:12 -06:00
{
2022-09-18 23:23:50 -06:00
resetText();
2022-09-18 22:04:12 -06:00
}
}
}
2022-09-18 23:23:50 -06:00
private void resetText()
{
myTitle.text = "";
myDesc.text = "";
check = false;
}
2022-09-18 22:04:12 -06:00
}