gimm270_07_photogallery/Assets/Scripts/Proximity.cs

59 lines
1.5 KiB
C#

using UnityEngine;
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;
public float minDistance = 4.0f;
public float maxDistance = 5.0f;
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");
message2 = GameObject.FindWithTag("Description");
myTitle = message1.GetComponent<Text>();
myTitle.text = "";
myDesc = message2.GetComponent<Text>();
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);
if (dist < minDistance)
{
myTitle.text = newTitle;
myDesc.text = newDesc;
check = true;
}
if (dist > maxDistance && check)
{
resetText();
}
}
}
private void resetText()
{
myTitle.text = "";
myDesc.text = "";
check = false;
}
}