I’ve looked everywhere but couldn’t find it.
is there a way to make an interaction button within proximity of an object?
example: if i walk up to a note, i want it to show: [E: Interact] (image).
I’ve looked everywhere but couldn’t find it.
is there a way to make an interaction button within proximity of an object?
example: if i walk up to a note, i want it to show: [E: Interact] (image).
For my game I use a Raycast, however this may not work in all game types as my game is a First Person 3D game, a Raycast points a Ray (essentially a invisible laser) at a predetermined position (in my case at the centre point of the camera/screen, however if your game is 3rd person you may want the raycast coming from the characters head, not the centre point of the camera, or if your game is 2D you may want the raycast being positioned over the mouse) so its hard when there is so many different ways to go about doing this for different game types.
EDIT: I should note that this is more a ‘LOOK AT’ type of interaction rather than an ‘AREA’ type interaction, it depends if you only want the player to interact with things when they are looking directly at said thing, or if you want to allow players to interact with things even if they are facing away from said thing, though again it depends on your games play style and the type of the game you are making.
When using a raycast you can mark specific objects with Tags so for example your Note object would need a Box Collider and have a Tag of ‘note’ for example (which the tag can be found just under the objects name in the inspector).
If you haven’t already got an Interaction Script you should create one to use and modify the below code example if you think this may be of use in your circumstance - of course you will need to modify this for your use case and for optimisation for your game as this is only an example but feel free to use:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InteractExample : MonoBehaviour
{
float raycastDistance = 3; //Adjust to suit your use case
public Text interactText; //Create GUI Canvas on your scene if you havnt already and a UI Text Element in a suitable location on your screen and apply it to this Text variable
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // This creates a 'ray' at the Main Camera's Centre Point essentially the centre of the users Screen
RaycastHit hit; //This creates a Hit which is used to callback the object that was hit by the Raycast
if (Physics.Raycast(ray, out hit, raycastDistance)) //Actively creates a ray using the above set perameters at the predeterminded distance
{
//Item Raycast Detection
if (hit.collider.CompareTag("note"))//Checking if the Raycast has hit a collider with the tag of note
{
interactText.text = "Press [E] to read the note."; //Setting the Interaction Text to let the player know they are now hovering an interactable object
if (Input.GetKeyDown(KeyCode.E))//Check if the player has pressed the Interaction button
{
Debug.Log("[E] Was Pressed while looking at a note, lets open the note.");
//Add your Note Method/UI here - for example (if the note has a script attached to it)
noteScript script = hit.collider.GetComponent<noteScript>();
if(script != null)
{
if (script.noteIsOpen)
{
//Enable Player Movement and Camera Movement
script.CloseNote();
}
else if (!script.notIsOpen)
{
//Disable Player Movement and Camera Movement
script.OpenNote();
}
}
}
}
else if (hit.collider.CompareTag("NEW_OBJECT_TAG_HERE"))
{
//Add a different object here if you choose to want more objects to be able to be detected by the Raycast such as doors etc
}
else //If nothing at all with an above tag was hit with the Raycast within the specified distance then run this
{
if(interactText.text != "")//If the interactText is not already set as nothing then set it to nothing - this is to help optimise and save from constantly spamming this request
{
interactText.text = ""; //Removing the text as nothing was detected by the raycast
}
}
}
}
}
Are you wanting to know how to put text on the screen, or how to make it so that when the player presses the right button they read the note?