Making text appear by pressing a button, only when player is close to the object
Hi! I've been trying to find a solution to my problem for a while now, but everything I'm finding is focused on 3D instead of 2D.
This is something what I want the process to look like:
player approaches object
animation appears over object
Player presses button
Text appears
I'm not really sure what to do. Can someone point me in the right direction? Maybe link me to some tutorials that cover these sort of processes? I'm still pretty new to all of this, if this question is in the wrong area, please let me know.
Here's a picture, in case someone needs a visual. Excuse the Paint, I'm away from my work computer.
Answer by UnityCoach · Nov 05, 2018 at 08:38 PM
First, you want to detect the distance to the player, and trigger an animation when it's within range. You want to hold this "state" and use it again to take input and activate the text object.
[SerializeField] float _triggerDistance = 1f; // the distance to trigger at
[SerializeField] GameObject _textToDisplay; // the text object to display
[SerializeField] Animator _animator; // the Animator component to set "bool" of
GameObject _player;
float _distance;
public float distance
{
get { return _distance; }
set
{
if (value != _distance) // prevents triggering things when value doesn't really change
{
_distance = value; // update value
playerIsClose = _distance <= _triggerDistance; // compare distance and assign bool value
}
}
}
bool _playerIsClose;
public bool playerIsClose
{
get { return _playerIsClose; }
set
{
if (value == _playerIsClose)
return; // return if value hasn't changed
_playerIsClose = value;
_animator.SetBool("PlayerIsClose", _playerIsClose); // trigger animator bool parameter
}
}
void Awake ()
{
_player = GameObject.FindWithTag("Player"); // make sure you tagged the Player with proper tag
}
void Update ()
{
// updating the distance from player, eventually triggering the animation
distance = Vector3.Distance(transform.position, _player.transform.position);
// catch input if playerIsClose is true
if (playerIsClose && Input.GetButtonUp ("Fire1"))
_textToDisplay.SetActive(_playerIsClose); // activate text
}
This will work fine, unless you have several objects within range, in which case you want to filter which object is closer, the more in the center of the screen or any other criteria to tell which one will be "triggered".
Here's a video showing the process.
It's from my Introduction to VR course available here.
Hope this helps.
Also, like your picture suggests, you can use a Trigger.
In which case, you'll simply make use of OnTriggerEnter()
and OnTriggerExit()
.
void OnTriggerEnter (Collider other)
{
if (!other.gameObject.CompareTag("Player"))
return; // return if it's not the player
playerIsClose = true;
}
void OnTriggerExit (Collider other)
{
if (!other.gameObject.CompareTag("Player"))
return; // return if it's not the player
playerIsClose = false;
}
Oh, and if you want to do this in 2D, it works the same, except if you want to use triggers, you'll need to use OnTriggerEnter2D (Collider2D other)
.
Thanks a ton! This pointed me in the right direction. I went ahead and subbed to your youtube channel.
Thanks again!