cassettecoroner

[Question] Unity3d first person development

Recommended Posts

Hey everybody, I've been dipping my toe into 3d development for the first time this past week using Unity and I've come across a problem I can't seem to solve through youtube tutorials and unityAnswers...

 

I can't get a simple "read note" mechanic working so that I can display appropriate text on the gui when the player clicks on an object in the world. I figured I'd use a Raycast solution so that I could broaden the variety of interaction when I've got some more practice under my belt, but I can't get the damn thing working! 

 

If need be I can pastebin the code I'm using, but it's honestly a mess. Is this as simple a problem as it seems it should be or am I in over my head?

 

Thanks in advance for any help.

Share this post


Link to post
Share on other sites

I'm looking at doing something similar in the future. I was thinking of having an object that's always at an appropriate spot in front of the camera that I can programmatically turn off and on as well as change the text of.

Share this post


Link to post
Share on other sites

I haven't done this, but if I was to try this is how I would approach it.

I would have a gameobject that isn't attached to the canvas at all that enables the canvas when a raycast returns a hit. The gameobject would have a script attached that includes a string that can be accessed by the canvas in my order to determine the text which is displayed.

Share this post


Link to post
Share on other sites

If I'm reading this correctly, I think what you are trying to do is click something in the world, then have that appear on the GUI?  kind of like how you can pick up an item and inspect it or rotate it around like in gone home?  

 

If so, and there isn't really a place anyone tells you this, first person games often use a series of cameras for their rendering.  The next time you are playing a first person shooter, walk up directly into a wall and equip the biggest gun you have.  If you then look at this from another player's perspective, the gun will be clipping through the wall, but the first person player won't see that.  The reason is that there is a separate camera for rendering high definition objects such as the gun or the player's hands.  It might look like the thing you are picking up is just flying into a viewable space on your screen, but in reality it's a bit of smoke and mirrors.  The object may fly up the screen or perform some other action, but in reality the object you are looking at is either an entirely different one, or being rendered by a separate camera specifically designed to display things in high detail.

Share this post


Link to post
Share on other sites

I think it's handled differently in different games. In your FPS example, instead of having a multiple cameras, some games have a second version of the gun model which can only be seen by the player, and the actual model that everyone else sees is hidden from the player.

Share this post


Link to post
Share on other sites

Any more details you can provide would be helpful! Are you currently able to click on the note and select it and you're just struggling with the displaying text part? Are you having trouble selecting the note? Your code might be helpful, but maybe some back-of-the-envelope sketches would be more helpful to convey what the core problem is.

Share this post


Link to post
Share on other sites

Sorry for the late response - Hope everyone had a great end of the year!

 

I'll try and be more clear and I'll include a pastebin of my in-progress code at the end.

 

In the practice game I am making while I learn I've littered several simple notebook objects around my game environment. When the player clicks on one of the notebooks (the game is in first person) I would like text to be displayed to the player as if they were reading a note that was left inside of the notebook they just clicked on. It doesn't need to be anything fancy.

 

The problem seems to be in getting the text to show up on the screen. The raycast functions correctly (there were initially more debug logs when I was testing the cast itself).

 

Here's the code I'm working with: http://pastebin.com/r7CJ8g6G

Share this post


Link to post
Share on other sites

I'm not positive this will work, but I think part of the problem is that you are treating the Text component as the string value of that component. The string is actually Text.text

I know that is counter-inntuitive.

So I'd try

RaycastHit.GetComponent<Text>().text;

I don't know how far along you are with implementing a text system and how much you would want to learn about the new UI system, but I just want you to know that a new UI system exists and this is where you can find information on how to use it.

https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-canvas

Share this post


Link to post
Share on other sites

If you haven't resolved this yet you might also want to try increasing the height of your Rect that you're constructing the label with. I did a quick test and only saw the very top pixels of the string that I used for testing with a height of 10. It appears that everything else was truncated. This testing was done in 2D mode so I'm not sure how much that changes things.

 

Hope that also helps!

 

Edit: spelling

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now