- Home /
how can i display a message to the player that they can interact with an object
I am making a game and i need to have the player see that they can interact with the object, i have a raycast set up to work with a few objects and the other things but i need them to be able to hover over an intractable object and tell them they can interact with it. If anyone could help that would be amazing!
Please provide some more detail. How should he player be notified? Should a message appear somewhere, or should the cursor change? What exactly is it that you are having problems with, the UI, the Raycasting, ...?
it would be just a pop up on the canvis to tell them that there is something infront of them that they can click to interact with
Answer by Jacky-Marley · Jan 24, 2017 at 10:23 AM
You can create a text in your canvas and access it as a GameObject. To detect if your player is in contact with the objects, use a triggered box collider so your player wont be blocked by it and Unity will detect the collision.
ex:
GameObject text;
void OnTriggerEnter ()
{
text.setActive(true);
text.GetComponent<Text>().text = "press E to use";
}
void OnTriggerStay ()
{
if (Input.GetKeyDown(Keycode.E)
{
Debug.Log("interaction");
}
}
void OnTriggerExit ()
{
text.setActive(false);
text.GetComponent<Text>().text = "";
}
would i use the box collider as if it were the pointing out of the player?
thas's in case of you want a select indicator in a game like Third Person, but if the player don't watch the object, the text appear anyway because you're in the area
if you want to make the message appear only when the player watch the object, look at the code below
RaycastHit myhit = new RaycastHit();
Ray myray = new Ray();
myray = Camera.main.ScreenPointToRay(Screen.Width / 2, Screen.height / 2);
if (!Physics.Raycast(myray, out myhit, 1000.0f))
{
if( //enter condition wanted)
{
text.setActive(true);
text.GetComponent<Text>().text = "text needed";
}
else
{
text.setActive(false);
text.GetComponent<Text>().text = "";
}
}
in this case, when the player look the object and the distance is not far, the text will appear else, the text disappear
Alright that sounds good. What would i put this in? my raycast script or make a separate one and apply it to each object i interact with?
add this script to your player script, for each object you want to interact, add a tag like "Object" and after in the condition you can make something like
if (!Physics.Raycast(myray, out myhit, 1000.0f))
{
if( myhit.gameObject.tag == "Object")
{
text.setActive(true);
text.GetComponent<Text>().text = "text needed";
}
else
{
text.setActive(false);
text.GetComponent<Text>().text = "";
}
}
Alright that sounds good. What would i put this in? my raycast script or make a separate one and apply it to each object i interact with?
i want to use this in my game but my game is android rpg game and they have button. how to change the interaction using a button?
i want to change the if (Input.GetKeyDown(Keycode.E) to using my button
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Interaction with various objects 0 Answers
Reload clips on Gun-script not applied properly 1 Answer
Clicking on an Object to Make it the Variable Target 1 Answer